<commit_before>User = require('../../../server/models/user')
Bucket = require('../../../server/models/bucket')
db = require('../../../server/lib/database')
{assert} = require('chai')

describe 'User', ->
  bucket = null
  user = null

  beforeEach (done) ->
    Bucket.create { name: 'Images', slug: 'images' }, (e, b) ->
      throw e if e
      bucket = b
      User.create { name: 'Bucketer', email: '<EMAIL>', password: '<PASSWORD>' }, (e, u) ->
        throw e if e
        user = u

        done()

  afterEach (done) ->
    db.connection.db.dropDatabase(done)

  describe 'password virtual', ->
    describe 'updating password', ->
      it 'updates password', (done) ->
        p = '<PASSWORD>'
        user.password = p
        user.save ->
          assert.isTrue(user.authenticate(p))
          assert.isNull(user.password)
          done()

    describe 'when password is missing', ->
      it 'returns an error', (done) ->
        User.create { name: 'Bucketer', email: '<EMAIL>' }, (e, u) ->
          assert.equal(e.errors.password.message, 'Password is required')
          done()

    describe 'when password is invalid', ->
      it 'returns an error', (done) ->
        User.create { name: 'Bucketer', email: '<EMAIL>', password: '<PASSWORD>' }, (e, u) ->
          assert.equal(e.errors.password.message, 'Your password must be between 6–20 characters and include a number')
          done()

  describe '#upsertRole', ->
    describe 'when a role is passed', ->
      describe 'when user does not have the role', ->
        it 'adds the global role', (done) ->
          user.upsertRole 'administrator', ->
            assert.lengthOf(user.roles, 1)
            user.upsertRole 'administrator', -><commit_msg>Add email validation tests<commit_after>User = require('../../../server/models/user')
Bucket = require('../../../server/models/bucket')
db = require('../../../server/lib/database')
{assert} = require('chai')

describe 'User', ->
  bucket = null
  user = null

  beforeEach (done) ->
    Bucket.create { name: 'Images', slug: 'images' }, (e, b) ->
      throw e if e
      bucket = b
      User.create { name: 'Bucketer', email: '<EMAIL>', password: '<PASSWORD>' }, (e, u) ->
        throw e if e
        user = u

        done()

  afterEach (done) ->
    db.connection.db.dropDatabase(done)

  describe 'email attribute', ->
    describe 'when email is valid', ->
      it 'validates', (done) ->
        user.email = '<EMAIL>'
        user.save (e, u) ->
          assert.isNull(e)
          done()

    describe 'when email is valid', ->
      it 'returns an error', (done) ->
        user.email = 'invalid'
        user.save (e, u) ->
          assert.equal(e.errors.email.message, 'Not a valid email adress')
          done()

  describe 'password virtual', ->
    describe 'updating password', ->
      it 'updates password', (done) ->
        p = '<PASSWORD>'
        user.password = p
        user.save ->
          assert.isTrue(user.authenticate(p))
          assert.isNull(user.password)
          done()

    describe 'when password is missing', ->
      it 'returns an error', (done) ->
        User.create { name: 'Bucketer', email: '<EMAIL>' }, (e, u) ->
          assert.equal(e.errors.password.message, 'Password is required')
          done()

    describe 'when password is invalid', ->
      it 'returns an error', (done) ->
        User.create { name: 'Bucketer', email: '<EMAIL>', password: '<PASSWORD>' }, (e, u) ->
          assert.equal(e.errors.password.message, 'Your password must be between 6–20 characters and include a number')
          done()

  describe '#upsertRole', ->
    describe 'when a role is passed', ->
      describe 'when user does not have the role', ->
        it 'adds the global role', (done) ->
          user.upsertRole 'administrator', ->
            assert.lengthOf(user.roles, 1)
            user.upsertRole 'administrator', ->
{'fraction_non_alphanumeric': 0.1654308361032984, 'fraction_numerical': 0.002045512656609563, 'mean_word_length': 2.209187858900738, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '643464', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1142, 'n_words': 339}