<commit_before>{ daisy
  expect
  request
  generateRandomEmail
  generateRandomString
  generateRandomUsername
  checkBongoConnectivity }          = require '../../../../testhelper'
{ withConvertedUser }               = require '../../../../../workers/social/testhelper'
{ withConvertedUserAndApiToken }    = require '../../../../../workers/social/testhelper/models/apitokenhelper'
{ generateCreateUserRequestParams } = require '../../../../testhelper/handler/createuserhelper'

JUser = require '../../../../models/user'

beforeTests = -> before (done) ->

  checkBongoConnectivity done


# here we have actual tests
runTests = -> describe 'server.handlers.api.createuser', ->

  it 'should send HTTP 401 if Authorization: Bearer header is not set', (done) ->

    createUserRequestParams = generateCreateUserRequestParams
      headers : { Authorization : '' }

    request.post createUserRequestParams, (err, res, body) ->
      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 401
      expect(body).to.be.equal 'unauthorized request'
      done()


  it 'should send HTTP 400 if api token is non-existent', (done) ->

    createUserRequestParams = generateCreateUserRequestParams()

    request.post createUserRequestParams, (err, res, body) ->
      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 400
      expect(body).to.be.equal 'invalid token!'
      done()


  it 'should send HTTP 400 if email is not set', (done) ->

    options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
    withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->
      createUserRequestParams = generateCreateUserRequestParams
        headers  : { Authorization : "Bearer #{apiToken.code}" }
        body     : { email  : '' }

      request.post createUserRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 400
        expect(body).to.be.equal 'invalid request'
        done()


  it 'should send HTTP 400 if username is already in use', (done) ->

    options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
    withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->
      createUserRequestParams = generateCreateUserRequestParams
        headers  : { Authorization : "Bearer #{apiToken.code}" }
        body     : { username : userFormData.username }

      request.post createUserRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 400
        expect(body).to.be.equal 'username is not available'
        done()


  it 'should send HTTP 400 if username is a banned one', (done) ->

    options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
    withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->
      createUserRequestParams = generateCreateUserRequestParams
        headers  : { Authorization : "Bearer #{apiToken.code}" }
        body     : { username : JUser.bannedUserList[0] }

      request.post createUserRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 400
        expect(body).to.be.equal 'username is not available'
        done()


  it 'should send HTTP 400 if email is in use', (done) ->

    options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
    withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->
      createUserRequestParams = generateCreateUserRequestParams
        headers  : { Authorization : "Bearer #{apiToken.code}" }
        body     : { email : userFormData.email }

      # api token creator tries to register himself
      request.post createUserRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 400
        expect(body).to.be.equal 'Email is already in use!'
        done()


  it 'should send HTTP 400 if given email is not in allowed domains', (done) ->

    options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
    withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->

      username = generateRandomUsername()
      createUserRequestParams = generateCreateUserRequestParams
        headers  : { Authorization : "Bearer #{apiToken.code}" }
        body     : { username, email : generateRandomEmail 'yandex.com' }

      expectedBody = 'Your email domain is not in allowed domains for this group'
      request.post createUserRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 400
        expect(body).to.be.equal expectedBody
        done()


  describe 'when request is valid', ->

    it 'should send HTTP 200 and create user with username provided', (done) ->

      options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
      withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->

        username = generateRandomUsername()
        password = <PASSWORD> = <PASSWORD>()

        createUserRequestParams = generateCreateUserRequestParams
          headers  : { Authorization : "Bearer #{apiToken.code}" }
          body     : { username }

        # creating user
        request.post createUserRequestParams, (err, res, body) ->
          expect(err).to.not.exist
          expect(res.statusCode).to.be.equal 200
          expect(body).to.contain username
          done()

    it 'should send HTTP 200 and create user without username provided', (done) ->

      options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
      withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->

        password = <PASSWORD> = <PASSWORD>()
<commit_msg>Server: Tests: add test case for api token disabled case<commit_after>{ daisy
  expect
  request
  generateRandomEmail
  generateRandomString
  generateRandomUsername
  checkBongoConnectivity }          = require '../../../../testhelper'
{ withConvertedUser }               = require '../../../../../workers/social/testhelper'
{ withConvertedUserAndApiToken }    = require '../../../../../workers/social/testhelper/models/apitokenhelper'
{ generateCreateUserRequestParams } = require '../../../../testhelper/handler/createuserhelper'

JUser = require '../../../../models/user'

beforeTests = -> before (done) ->

  checkBongoConnectivity done


# here we have actual tests
runTests = -> describe 'server.handlers.api.createuser', ->

  it 'should send HTTP 401 if Authorization: Bearer header is not set', (done) ->

    createUserRequestParams = generateCreateUserRequestParams
      headers : { Authorization : '' }

    request.post createUserRequestParams, (err, res, body) ->
      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 401
      expect(body).to.be.equal 'unauthorized request'
      done()


  it 'should send HTTP 400 if api token is non-existent', (done) ->

    createUserRequestParams = generateCreateUserRequestParams()

    request.post createUserRequestParams, (err, res, body) ->
      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 400
      expect(body).to.be.equal 'invalid token!'
      done()


  it 'should send HTTP 400 if email is not set', (done) ->

    options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
    withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->
      createUserRequestParams = generateCreateUserRequestParams
        headers  : { Authorization : "Bearer #{apiToken.code}" }
        body     : { email  : '' }

      request.post createUserRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 400
        expect(body).to.be.equal 'invalid request'
        done()


  it 'should send HTTP 400 if username is already in use', (done) ->

    options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
    withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->
      createUserRequestParams = generateCreateUserRequestParams
        headers  : { Authorization : "Bearer #{apiToken.code}" }
        body     : { username : userFormData.username }

      request.post createUserRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 400
        expect(body).to.be.equal 'username is not available'
        done()


  it 'should send HTTP 400 if username is a banned one', (done) ->

    options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
    withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->
      createUserRequestParams = generateCreateUserRequestParams
        headers  : { Authorization : "Bearer #{apiToken.code}" }
        body     : { username : JUser.bannedUserList[0] }

      request.post createUserRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 400
        expect(body).to.be.equal 'username is not available'
        done()


  it 'should send HTTP 400 if email is in use', (done) ->

    options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
    withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->
      createUserRequestParams = generateCreateUserRequestParams
        headers  : { Authorization : "Bearer #{apiToken.code}" }
        body     : { email : userFormData.email }

      # api token creator tries to register himself
      request.post createUserRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 400
        expect(body).to.be.equal 'Email is already in use!'
        done()


  it 'should send HTTP 400 if given email is not in allowed domains', (done) ->

    options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
    withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->

      username = generateRandomUsername()
      createUserRequestParams = generateCreateUserRequestParams
        headers  : { Authorization : "Bearer #{apiToken.code}" }
        body     : { username, email : generateRandomEmail 'yandex.com' }

      expectedBody = 'Your email domain is not in allowed domains for this group'
      request.post createUserRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 400
        expect(body).to.be.equal expectedBody
        done()


  it 'should send HTTP 403 if group.isApiTokenEnabled is not true', (done) ->

    options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
    withConvertedUserAndApiToken options, ({ userFormData, apiToken, group }) ->

      # setting api token availability false for the group
      group.setApiTokenAvailability false, (err) ->
        expect(err).to.not.exist

        username = generateRandomUsername()
        createUserRequestParams = generateCreateUserRequestParams
          headers  : { Authorization : "Bearer #{apiToken.code}" }
          body     : { username, email : generateRandomEmail 'yandex.com' }

        expectedBody = 'api token usage is not enabled for this group'
        request.post createUserRequestParams, (err, res, body) ->
          expect(err).to.not.exist
          expect(res.statusCode).to.be.equal 403
          expect(body).to.be.equal expectedBody
          done()


  describe 'when request is valid', ->

    it 'should send HTTP 200 and create user with username provided', (done) ->

      options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
      withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->

        username = generateRandomUsername()
        password = passwordConfirm = <PASSWORD>()

        createUserRequestParams = generateCreateUserRequestParams
          headers  : { Authorization : "Bearer #{apiToken.code}" }
          body     : { username }

        # creating user
        request.post createUserRequestParams, (err, res, body) ->
          expect(err).to.not.exist
          expect(res.statusCode).to.be.equal 200
          expect(body).to.contain username
          done()

    it 'should send HTTP 200 and create user without username provided', (done) ->

      options = { createGroup : yes, groupData : { isApiTokenEnabled : yes } }
      withConvertedUserAndApiToken options, ({ userFormData, apiToken }) ->

        password = passwordConfirm = <PASSWORD>()

{'fraction_non_alphanumeric': 0.1138804295560186, 'fraction_numerical': 0.008815515306940214, 'mean_word_length': 2.836151245004611, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 71, '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': '3830090', 'n_tokens_mistral': 3550, 'n_tokens_neox': 3347, 'n_words': 897}