process.env.NODE_ENV = 'test';
const mongoose = require('mongoose');

let chai = require('chai');
let chaiHttp = require('chai-http');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const PassThrough = require('stream').PassThrough;
const server = require('../index');
let should = chai.should();
const nconf = require('../config/nconf.js');
const spotifyWebApi = require('spotify-web-api-node');

const loginController = require('../controllers/loginController');
const sinonStub = require('./stub');

chai.use(chaiHttp);
chai.use(sinonChai);

describe('Login', () => {
  let request, response;
  beforeEach(() => {
    request = {
      spotifyApi: new spotifyWebApi({
        clientId : 'SPOTIFY_CLIENT_ID',
        clientSecret : 'SPOTIFY_CLIENT_SECRET',
        redirectUri : 'SPOTIFY_REDIRECT_URI'
      })
    };

    const stubAuth = sinonStub(request.spotifyApi, 'authorizationCodeGrant',
      {
        statusCode: 200,
        body: { access_token: 'token' }
      });

    const stubGetMe = sinonStub(request.spotifyApi, 'getMe',
      {
        body: {
          country: 'ES',
          display_name: 'Ro Rey',
          email: 'someone@gmail.com',
          external_urls: { spotify: 'https://open.spotify.com/user/djdjdjdj' },
          followers: { href: null, total: 0 },
          href: 'https://api.spotify.com/v1/users/djdjdjdj',
          id: 'ou2o3iu453245',
          images: [ [Object] ],
          product: 'open',
          type: 'user',
          uri: 'spotify:user:djdjdjdj'
        }
      });

    const stubGetUserPlaylists = sinonStub(request.spotifyApi, 'getUserPlaylists', { body: { items: [{id: 3838383}, {id: 393939}] }});

    const stubGetPlaylistTracks = sinonStub(request.spotifyApi, 'getPlaylistTracks', { body: { items: [{track: {id: 2423}}, {track: {id: 7474}}, {track: {id: 92929}}, {track: {id: 94949}}]}});

    response = {};
  });

  it('login should succeed given proper user credentials', (done) => {
    request.body = {
      code: '83838383k3i'
    };

    response.send = (obj) => {
      try {
        response.status.should.be.eq(200);
        obj.should.be.a('object');
        obj.should.have.property('userToken');
        obj.should.have.property('name');
        done();
      } catch (err) {
        done(err);
      }

    };

    loginController.login(request, response);

  });

  it('login should fail without an authorization code', (done) => {
    request.body = {};

    response.sendStatus = (status) => {
      try {
        status.should.be.eq(400);
        done();
      } catch (err) {
        done(err);
      }
    };

    loginController.login(request, response);

  });

});

@@ -490,21 +490,16 @@
 ;%0Aconst 
-sinon
 Stub = r
@@ -559,16 +559,87 @@
 Chai);%0A%0A
+let stubAuth, stubGetMe, stubGetUserPlaylists, stubGetPlaylistTracks;%0A%0A
 describe
@@ -645,16 +645,17 @@
 e('Login
+:
 ', () =%3E
@@ -907,30 +907,24 @@
     %7D;%0A%0A    
-const 
 stubAuth = s
@@ -922,21 +922,27 @@
 bAuth = 
-sinon
+Stub.create
 Stub(req
@@ -1068,30 +1068,24 @@
    %7D);%0A%0A    
-const 
 stubGetMe = 
@@ -1084,21 +1084,27 @@
 GetMe = 
-sinon
+Stub.create
 Stub(req
@@ -1612,30 +1612,24 @@
    %7D);%0A%0A    
-const 
 stubGetUserP
@@ -1635,29 +1635,35 @@
 Playlists = 
-sinon
+Stub.create
 Stub(request
@@ -1752,22 +1752,16 @@
 );%0A%0A    
-const 
 stubGetP
@@ -1776,21 +1776,27 @@
 racks = 
-sinon
+Stub.create
 Stub(req
@@ -1960,32 +1960,229 @@
 se = %7B%7D;%0A  %7D);%0A%0A
+  afterEach(() =%3E %7B%0A    Stub.removeStub(stubAuth);%0A    Stub.removeStub(stubGetMe);%0A    Stub.removeStub(stubGetUserPlaylists);%0A    Stub.removeStub(stubGetPlaylistTracks);%0A%0A    response = %7B%7D;%0A  %7D);%0A%0A
   it('login shou

update test.js to accommodate new version of stub.js