const { sandbox } = require('sinon');
const { expect } = require('chai');
const { permissions } = require('src/api');
const { authConf } = require('src/auth-utils');
const auth = require('src/auth');


describe('api.permissions', () => {
  beforeEach(() => {
    this.sandbox = sandbox.create();
  });

  afterEach(() => {
    this.sandbox.restore();
  });

  describe('create', () => {
    it('should create permissions', () => {
      const expected = {
        id: 22,
        type: 'project:write',
        namespace: '_numi_',
        object_id: 21
      };

      this.sandbox.stub(auth.teams, 'addPermission')
        .withArgs(23, {
          type: 'project:write',
          namespace: '_numi_',
          object_id: 21
        }, {
          conf: authConf({
            type: 'token',
            token: 'abc'
          })
        })
        .returns(expected);

      const res = permissions.create(23, {
        type: 'project:write',
        object_id: 21
      }, {
        namespace: '_numi_',
        auth: {
          type: 'token',
          token: 'abc'
        }
      });

      expect(res).to.deep.equal(expected);
    });
  });

  describe('remove', () => {
    it('should throw a NotImplementedError', () => {
      const expected = {
        id: 22,
        type: 'project:write',
        namespace: '_numi_',
        object_id: 21
      };

      this.sandbox.stub(auth.teams, 'removePermission')
        .withArgs(23, 21)
        .returns(expected);

      const res = permissions.remove(23, 21, {
        auth: {
          type: 'token',
          token: 'abc'
        }
      });

      expect(res).to.deep.equal(expected);
    });
  });
});

@@ -169,16 +169,19 @@
 nst auth
+Api
  = requi
@@ -552,32 +552,107 @@
 d: 21%0A      %7D;%0A%0A
+      const auth = %7B%0A        type: 'token',%0A        token: 'abc'%0A      %7D;%0A%0A
       this.sandb
@@ -655,32 +655,35 @@
 andbox.stub(auth
+Api
 .teams, 'addPerm
@@ -845,73 +845,12 @@
 onf(
-%7B%0A            type: 'token',%0A            token: 'abc'%0A          %7D
+auth
 )%0A  
@@ -1039,69 +1039,8 @@
 auth
-: %7B%0A          type: 'token',%0A          token: 'abc'%0A        %7D
 %0A   
@@ -1312,32 +1312,107 @@
 d: 21%0A      %7D;%0A%0A
+      const auth = %7B%0A        type: 'token',%0A        token: 'abc'%0A      %7D;%0A%0A
       this.sandb
@@ -1423,16 +1423,19 @@
 tub(auth
+Api
 .teams, 
@@ -1478,16 +1478,40 @@
 s(23, 21
+, %7Bconf: authConf(auth)%7D
 )%0A      
@@ -1583,94 +1583,16 @@
 1, %7B
-%0A        auth: %7B%0A          type: 'token',%0A          token: 'abc'%0A        %7D%0A      
+auth
 %7D);%0A
-%0A
     

Clean up api.permissions tests