{"size": 2515, "ext": "js", "max_stars_repo_path": "server/tests/recruiter.test.js", "max_stars_repo_name": "ohtu-rekry/recruitment-tool", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/tests/recruiter.test.js", "max_issues_repo_name": "ohtu-rekry/recruitment-tool", "max_issues_repo_issues_event_min_datetime": "2018-09-07T12:00:13.000Z", "max_issues_repo_issues_event_max_datetime": "2018-12-16T17:33:39.000Z", "max_forks_repo_path": "server/tests/recruiter.test.js", "max_forks_repo_name": "ohtu-rekry/recruitment-tool", "max_forks_repo_forks_event_min_datetime": "2018-09-10T08:51:07.000Z", "max_forks_repo_forks_event_max_datetime": "2018-09-10T08:51:07.000Z"}
{"max_stars_count": null, "max_issues_count": 77, "max_forks_count": 1, "avg_line_length": 25.404040404, "max_line_length": 96, "alphanum_fraction": 0.607554672}
const supertest = require('supertest')
const { app, server } = require('../src/server')
const api = supertest(app)
const { Recruiter, sequelize, Sequelize } = require('../db/models')
const bcrypt = require('bcryptjs')

beforeAll(async () => {
  await sequelize.sync({ logging: false })
    .catch(() => {
      console.log('Another model synchronizing process has already started')
    })
})

describe('CREATE RECRUITER USER', async () => {
  const newRecruiter = {
    username: 'testi',
    password: 'hunter2'
  }
  const testRecruiter = {
    username: 'recruiter-test-username',
    password: 'recruiter-test-password'
  }
  let token

  beforeAll(async () => {
    await Recruiter.create({
      username: testRecruiter.username,
      password: await bcrypt.hash(testRecruiter.password, 10)
    })

    const response = await api.post('/api/login').send(testRecruiter)
    token = `Bearer ${response.body.token}`
  })

  beforeEach(async () => {
    await Recruiter.destroy({
      where: {
        username: newRecruiter.username
      }
    })
  })

  test('a recruiter user can be created', async () => {
    await api
      .post('/api/recruiter')
      .set('authorization', token)
      .send(newRecruiter)
      .expect(200)
      .expect('Content-Type', /application\/json/)
  })

  test('two identical usernames can not be created', async () => {
    await api
      .post('/api/recruiter')
      .set('authorization', token)
      .send(newRecruiter)
      .expect(200)
      .expect('Content-Type', /application\/json/)

    const result = await api
      .post('/api/recruiter')
      .set('authorization', token)
      .send(newRecruiter)
      .expect(400)
      .expect('Content-Type', /application\/json/)

    expect(result.body).toEqual({ error: 'This username already exists.' })
  })

  test('a recruiter user with password less than 3 cannot be created', async () => {
    newRecruiter.password = 'hu'

    const result = await api
      .post('/api/recruiter')
      .set('authorization', token)
      .send(newRecruiter)
      .expect(400)
      .expect('Content-Type', /application\/json/)

    expect(result.body).toEqual({ error: 'password length must be at least 3 characters long' })
  })

  afterAll(async () => {
    await Recruiter.destroy({
      where: {
        username: {
          [Sequelize.Op.in]: [
            newRecruiter.username,
            testRecruiter.username
          ]
        }
      }
    })
    await server.close()
    await sequelize.close()
  })
})

