zoukankan      html  css  js  c++  java
  • [Node & Tests] Intergration tests for Authentication

    For intergration tests, always remember when you create a 'mass' you should aslo clean up the 'mass'.

    For example when you start the server, you need to close the server after the tests. See the post: http://www.cnblogs.com/Answer1215/p/7554662.html

    Also for authentication, when you create a new user for testing, you should also clean it up.

    There is a help function for tests, to create a new user:

    async function createNewUser(overrides) {
      const password = faker.internet.password()
      const userData = generateUserData(overrides)
      const {email, username} = userData
      const user = await api
        .post('users', {user: {email, password, username}})
        .then(getUser)
      return {
        user,
        cleanup() {
          return api.delete(`users/${user.username}`)
        },
      }
    }

    In the return value, it also provide a function to clean up the user.

    const api = axios.create({
      baseURL: 'http://localhost:3000/api',
    })
    
    describe('authenticated', () => {
      let cleanupUser
      beforeAll(async () => {
        const results = await createNewUser()
        cleanupUser = results.cleanup
        api.defaults.headers.common.authorization = `Token ${results.user.token}` // set default http header, add authorization for JWT token
      })
    
      afterAll(async () => {
        await cleanupUser()
        api.defaults.headers.common.authorization = ''
      })
    
    })
  • 相关阅读:
    二叉搜索树
    【树】List Leaves
    模板——dijkstra单源最短路
    余数求和——除法分块
    倍增——ST表
    线段树——内存池
    线段树——模板
    洛谷 P1498 南蛮图腾
    洛谷 P2199 最后的迷宫
    洛谷 P1495 中国剩余定理
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7555260.html
Copyright © 2011-2022 走看看