'use strict'
const Promise = require('bluebird')
const db = require('APP/db')
    , {User, Address} = db
    , {expect} = require('chai')

/* global describe it before afterEach */

describe('User', () => {
  before('Await database sync', () => db.didSync)
  afterEach('Clear the tables', () => db.truncate({ cascade: true }))

  describe('authenticate(plaintext: String) ~> Boolean', () => {
    let testUser

    before(done => {
      User.create({ email: 'test@gmail.com', password: 'ok' })
        .then(res => {
          testUser = res
          done()
        })
    })

    it('resolves true if the password matches', () => {
      testUser.authenticate('ok')
      .then(result => expect(result).to.be.true)
      .catch(err => console.error('error authenticating user password', err))
    })

    it("resolves false if the password doesn't match", () => {
      testUser.authenticate('not ok')
      .then(result => expect(result).to.be.false)
      .catch(err => console.error('error authenticating user password', err))
    })
  })

  describe('isGuest option method', () => {
    it('returns true if password does not exist', () => {
      User.create({email: 'tenk@gmail.com'})
        .then(guestUser => expect(guestUser.isGuest).to.be.true)
    })
  })

  describe('User Associations', () => {
    before(done => {
      const creatingUser = User.create({email: 'kido@kido.com'})
      const creatingAddress = Address.create({country: 'USA', firstName: 'Kido Kido', lastName: 'Kido', administrativeArea: 'NY', locality: 'NYC', postalZipCode: '12345', streetAddress: '123 Kido Lane'})

      Promise.all([creatingUser, creatingAddress])
      .spread((user, address) => {
        user.addShippingInfo([address])
        user.addBillingInfo([address])
        done()
      })
    })

    it('sets billing association correctly', () => {
      User.findOne({where: {
        email: 'kido@kido.com'
      }})
      .then(user => user.getBillingInfo())
      .then(billingInfo => {
        expect(billingInfo.length).to.equal(1)
        expect(billingInfo[0].fullName).to.equal('Kido Kido Kido')
      })
    })
  })
})

@@ -394,37 +394,54 @@
     
-let testUser%0A%0A    before(done
+it('resolves true if the password matches', ()
  =%3E 
@@ -523,145 +523,17 @@
 hen(
-res =%3E %7B%0A          testUser = res%0A          done()%0A        %7D)%0A    %7D)%0A%0A    it('resolves true if the password matches', () =%3E %7B%0A      testU
+user =%3E u
 ser.
@@ -546,25 +546,28 @@
 ticate('ok')
-%0A
+)%0A  
       .then(
@@ -611,164 +611,161 @@
     
-  .catch(err =%3E console.error('error authenticating user password', err))%0A    %7D)%0A%0A    it(%22resolves false if the password doesn't match%22, () =%3E %7B%0A      testU
+%7D)%0A%0A    it(%22resolves false if the password doesn't match%22, () =%3E %7B%0A      User.create(%7B email: 'test@gmail.com', password: 'ok' %7D)%0A        .then(user =%3E u
 ser.
@@ -786,17 +786,20 @@
 not ok')
-%0A
+)%0A  
       .t
@@ -844,86 +844,8 @@
 se)%0A
-      .catch(err =%3E console.error('error authenticating user password', err))%0A
     

test issues