"use strict";

var assert = require("assert");

var SolidBucket = require("../index");

var _require = require("./utils"),
  resolveUniversalApiWorkflow = _require.resolveUniversalApiWorkflow,
  simulateObjectCorruptionWorkflow = _require.simulateObjectCorruptionWorkflow,
  rejectMissingParametersWorkflow = _require.rejectMissingParametersWorkflow,
  rejectUniversalApiWorkflow = _require.rejectUniversalApiWorkflow;

var accessKeyId = process.env.DIGITALOCEAN_ACCESSKEYID;
var secretAccessKey = process.env.DIGITALOCEAN_SECRETACCESSKEY;

describe("DigitalOcean Spaces", function() {
  this.timeout(15000);

  describe("Universal API", function() {
    it("should test the happy path", function() {
      var bucketName =
        String(
          Math.floor(Math.random() * 200000000) *
            Math.floor(Math.random() * 200000000)
        ) + "tests";
      var filePath = "lib/test/testdata/binary.bin";
      var filenameTxt = "generated-txt.txt";

      var provider = new SolidBucket("digitalocean", {
        accessKeyId: accessKeyId,
        secretAccessKey: secretAccessKey
      });

      resolveUniversalApiWorkflow(provider, bucketName, filePath, filenameTxt);
    });

    it("should test against missing parameters", function() {
      var bucketName = "";
      var filename = "";

      var provider = new SolidBucket("digitalocean", {
        accessKeyId: accessKeyId,
        secretAccessKey: secretAccessKey
      });
      rejectMissingParametersWorkflow(provider, bucketName, filename);
    });

    it("should test against missing auth options", function() {
      assert.throws(function() {
        new SolidBucket("digitalocean", {
          accessKeyId: "",
          secretAccessKey: ""
        });
      }, Error);
    });

    it("should test object upload corruptions", function() {
      var bucketName =
        String(
          Math.floor(Math.random() * 200000000) *
            Math.floor(Math.random() * 200000000)
        ) + "tests";
      var filename = "generated-during-tests.txt";

      var provider = new SolidBucket("digitalocean", {
        accessKeyId: accessKeyId,
        secretAccessKey: secretAccessKey
      });

      simulateObjectCorruptionWorkflow(provider, bucketName, filename);
    });

    it("should test the provider rejections", function() {
      var bucketName = "backup";
      var filename = "generated-during-tests.txt";

      var provider = new SolidBucket("digitalocean", {
        accessKeyId: accessKeyId,
        secretAccessKey: secretAccessKey
      });
      rejectUniversalApiWorkflow(provider, bucketName, filename);
    });
  });
});
