<commit_before>import { Config, configFrom } from '../../../config/config.js'
import { DB } from '../../../lib/db/db.js'
import { UserDB } from '../../db/user-db.js'
import { insertUserFix4 } from '../../db/user-db-fixture.js'
import { Express2 } from '../../../lib/express/express2.js'
import { SuperAgentTest } from 'supertest'
import { registerUserLoginApi } from './user-login-api.js'
import { Router } from 'express'
import { NOT_AUTHENTICATED } from '../register-form/user-form.js'
import { loginForTest, logoutForTest, User1Login } from './user-login-api-fixture.js'

describe('UserLoginApi', () => {

  let config: Config

  let db: DB
  let udb: UserDB
  let web: Express2
  let router: Router
  let request: SuperAgentTest

  beforeAll(async () => {
    config = configFrom('config/app-test.json')

    db = await DB.from(config).createDatabase()
    udb = UserDB.from(db)

    web = await Express2.from(config).start()
    registerUserLoginApi(web, udb)
    router = web.router
    request = web.spawnRequest()
  })

  afterAll(async () => {
    await web.close()
    await db.close()
  })

  describe('auto login', () => {
    it('init table', async () => {
      await udb.dropTable()
      await udb.createTable(false)
    })
    it('fill fix', async () => {
      await insertUserFix4(udb)
    })

    it('get login fails before login', async () => {
      const res = await request.get('/api/user/login').expect(200)
      expect(res.body.err).toEqual(NOT_AUTHENTICATED)
    })
    it('cookie should be empty', async () => {
      const res = await request.get('/api/cookies').expect(200)
      expect(res.body.email).toBe(undefined)
    })

    it('login', async () => {
      await loginForTest(request, User1Login, true)
    })
    it('get login works', async () => {
      const res = await request.get('/api/user/login').expect(200)
      expect(res.body.user.id).toBe(1)
    })
    it('cookies are filled', async () => {
      const res = await request.get('/api/cookies').expect(200)
      expect(res.body.email).toBe('<EMAIL>')
    })

    it('destroy session', async () => {
      await request.post('/api/destroy-session').expect(200)
    })
    it('get login works (autologin worked)', async () => {
      const res = await request.get('/api/user/login').expect(200)
      expect(res.body.user.id).toBe(1)
    })
    it('cookies still exist', async () => {
      const res = await request.get('/api/cookies').expect(200)
      expect(res.body.email).toBe('<EMAIL>')
    })

    it('logout', async () => {
      await logoutForTest(request)
    })
    it('get login fails', async () => {
      const res = await request.get('/api/user/login').expect(200)
      expect(res.body.err).toEqual(NOT_AUTHENTICATED)
    })
    it('cookies are empty', async () => {
      const res = await request.get('/api/cookies').expect(200)
      expect(res.body.email).toBe(undefined)
    })

    it('login 2', async () => {
      await loginForTest(request, User1Login, true)
    })
    it('get login works', async () => {
      const res = await request.get('/api/user/login').expect(200)
      expect(res.body.err).toBe(undefined)
    })

    it('change db email', async () => {
      await db.query('update user set email = "<EMAIL>" where id = ?', 1)
    })
    it('destroy session', async () => {
      await request.post('/api/destroy-session').expect(200)
    })
    it('get login fails (autologin failed)', async () => {
      const res = await request.get('/api/user/login').expect(200)
      expect(res.body.err).toEqual(NOT_AUTHENTICATED)
    })
    it('cookies are empty', async () => {
      const res = await request.get('/api/cookies').expect(200)
      expect(res.body.email).toBe(undefined)
    })
  })

})
<commit_msg>add empty line<commit_after>import { Config, configFrom } from '../../../config/config.js'
import { DB } from '../../../lib/db/db.js'
import { UserDB } from '../../db/user-db.js'
import { insertUserFix4 } from '../../db/user-db-fixture.js'
import { Express2 } from '../../../lib/express/express2.js'
import { SuperAgentTest } from 'supertest'
import { registerUserLoginApi } from './user-login-api.js'
import { Router } from 'express'
import { NOT_AUTHENTICATED } from '../register-form/user-form.js'
import { loginForTest, logoutForTest, User1Login } from './user-login-api-fixture.js'

describe('UserLoginApi', () => {

  let config: Config

  let db: DB
  let udb: UserDB

  let web: Express2
  let router: Router
  let request: SuperAgentTest

  beforeAll(async () => {
    config = configFrom('config/app-test.json')

    db = await DB.from(config).createDatabase()
    udb = UserDB.from(db)

    web = await Express2.from(config).start()
    registerUserLoginApi(web, udb)
    router = web.router
    request = web.spawnRequest()
  })

  afterAll(async () => {
    await web.close()
    await db.close()
  })

  describe('auto login', () => {
    it('init table', async () => {
      await udb.dropTable()
      await udb.createTable(false)
    })
    it('fill fix', async () => {
      await insertUserFix4(udb)
    })

    it('get login fails before login', async () => {
      const res = await request.get('/api/user/login').expect(200)
      expect(res.body.err).toEqual(NOT_AUTHENTICATED)
    })
    it('cookie should be empty', async () => {
      const res = await request.get('/api/cookies').expect(200)
      expect(res.body.email).toBe(undefined)
    })

    it('login', async () => {
      await loginForTest(request, User1Login, true)
    })
    it('get login works', async () => {
      const res = await request.get('/api/user/login').expect(200)
      expect(res.body.user.id).toBe(1)
    })
    it('cookies are filled', async () => {
      const res = await request.get('/api/cookies').expect(200)
      expect(res.body.email).toBe('<EMAIL>')
    })

    it('destroy session', async () => {
      await request.post('/api/destroy-session').expect(200)
    })
    it('get login works (autologin worked)', async () => {
      const res = await request.get('/api/user/login').expect(200)
      expect(res.body.user.id).toBe(1)
    })
    it('cookies still exist', async () => {
      const res = await request.get('/api/cookies').expect(200)
      expect(res.body.email).toBe('<EMAIL>')
    })

    it('logout', async () => {
      await logoutForTest(request)
    })
    it('get login fails', async () => {
      const res = await request.get('/api/user/login').expect(200)
      expect(res.body.err).toEqual(NOT_AUTHENTICATED)
    })
    it('cookies are empty', async () => {
      const res = await request.get('/api/cookies').expect(200)
      expect(res.body.email).toBe(undefined)
    })

    it('login 2', async () => {
      await loginForTest(request, User1Login, true)
    })
    it('get login works', async () => {
      const res = await request.get('/api/user/login').expect(200)
      expect(res.body.err).toBe(undefined)
    })

    it('change db email', async () => {
      await db.query('update user set email = "<EMAIL>" where id = ?', 1)
    })
    it('destroy session', async () => {
      await request.post('/api/destroy-session').expect(200)
    })
    it('get login fails (autologin failed)', async () => {
      const res = await request.get('/api/user/login').expect(200)
      expect(res.body.err).toEqual(NOT_AUTHENTICATED)
    })
    it('cookies are empty', async () => {
      const res = await request.get('/api/cookies').expect(200)
      expect(res.body.email).toBe(undefined)
    })
  })

})

{'fraction_non_alphanumeric': 0.1823278163374266, 'fraction_numerical': 0.013881473571809931, 'mean_word_length': 2.8504624871531345, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 55, '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': '3745287', 'n_tokens_mistral': 2472, 'n_tokens_neox': 2424, 'n_words': 490}