"use strict";

var assert = require("assert");

var fs = require("fs");
var slashes = require("slashes");

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

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

var applyCredentials = function applyCredentials(task) {
  var options = {
    type: "service_account",
    project_id: process.env.GCS_PROJECTID,
    private_key_id: process.env.GCS_PRIVATEKEYID,
    private_key: slashes.strip(process.env.GCS_PRIVATEKEY),
    client_email: process.env.GCS_CLIENTEMAIL,
    client_id: process.env.GCS_CLIENTID,
    auth_uri: "https://accounts.google.com/o/oauth2/auth",
    token_uri: "https://accounts.google.com/o/oauth2/token",
    auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
    client_x509_cert_url: process.env.GCS_CLIENTX509CERTURL
  };

  var optionsJson = JSON.stringify(options);
  fs.writeFile("gcs-credentials.json", optionsJson, "utf8", function() {
    var keyFilename = "gcs-credentials.json";

    task(keyFilename);
  });
};

describe("Google Cloud Storage", function() {
  this.timeout(15000);

  describe("Universal API", function() {
    it("should test the happy path", function() {
      function task(keyFilename) {
        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("gcs", {
          keyFilename: keyFilename
        });

        resolveUniversalApiWorkflow(
          provider,
          bucketName,
          filePath,
          filenameTxt
        );

        fs.unlinkSync(keyFilename);
      }

      applyCredentials(task);
    });

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

        var provider = new SolidBucket("gcs", {
          keyFilename: keyFilename
        });

        rejectMissingParametersWorkflow(provider, bucketName, filename);

        fs.unlinkSync(keyFilename);
      }

      applyCredentials(task);
    });

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

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

        var provider = new SolidBucket("gcs", {
          keyFilename: keyFilename
        });

        simulateObjectCorruptionWorkflow(provider, bucketName, filename);

        fs.unlinkSync(keyFilename);
      }

      applyCredentials(task);
    });

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

        var provider = new SolidBucket("gcs", {
          keyFilename: keyFilename
        });

        rejectUniversalApiWorkflow(provider, bucketName, filename);

        fs.unlinkSync(keyFilename);
      }

      applyCredentials(task);
    });
  });
});
