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 = ''
      })
    
    })
  • 相关阅读:
    JavaScripts广告轮播图以及定时弹出和定时隐藏广告
    JavaScript正则表达
    表单常用标签 和 属性
    html框架集
    Hbuilder 快捷键
    css 图片
    html input accept类型
    db2 sql
    js 数组排序
    html input size maxlength
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7555260.html
Copyright © 2011-2022 走看看