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 Stub = require('./stub');

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

let stubAuth, stubGetMe, stubGetUserPlaylists, stubGetPlaylistTracks;

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

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

    stubGetMe = Stub.createStub(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'
        }
      });

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

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

    response = {};
  });

  afterEach(() => {
    Stub.removeStub(stubAuth);
    Stub.removeStub(stubGetMe);
    Stub.removeStub(stubGetUserPlaylists);
    Stub.removeStub(stubGetPlaylistTracks);

    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);

  });

  it('login should fail without proper Spotify API credentials', (done) => {
    request = {
      body: {
        code: '83838383k3i'
      },
      spotifyApi: new spotifyWebApi({
        clientId : null,
        clientSecret : null,
        redirectUri : 'SPOTIFY_REDIRECT_URI'
      })
    };

    stubAuth.restore();

    stubAuth = Stub.createStub(request.spotifyApi, 'authorizationCodeGrant',
      {
        statusCode: 400,
        body: {}
      });

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

    loginController.login(request, response);

  });

});

@@ -3224,32 +3224,361 @@
      %7D)%0A    %7D;%0A%0A
+    response.sendStatus = (status) =%3E %7B%0A      try %7B%0A        status.should.be.eq(400);%0A        done();%0A      %7D catch (err) %7B%0A        done(err);%0A      %7D%0A    %7D;%0A%0A    loginController.login(request, response);%0A%0A  %7D);%0A%0A  it('login should fail if authorization fails', (done) =%3E %7B%0A    request.body = %7B%0A      code: '83838383k3i'%0A    %7D;%0A%0A
     stubAuth.res
@@ -3741,32 +3741,23 @@
 nse.send
-Status = (status
+ = (obj
 ) =%3E %7B%0A 
@@ -3767,32 +3767,41 @@
   try %7B%0A        
+response.
 status.should.be
@@ -3802,32 +3802,86 @@
 uld.be.eq(400);%0A
+        obj.statusCode.should.be.eq(response.status);%0A
         done();%0A
@@ -3921,32 +3921,33 @@
 e(err);%0A      %7D%0A
+%0A
     %7D;%0A%0A    logi

include test for authorization failure and update test for improper Spotify credentials