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 = ''
      })
    
    })
  • 相关阅读:
    float对内联元素和块元素的影响
    行内元素的困扰
    Nodejs同步和异步编程
    let、const、var的区别
    作用域
    SpringMVC
    MVC2新闻编译与发布
    JSP显示新闻
    servlet应用
    SWPU邮件登录界面的仿写(第二次作业)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7555260.html
Copyright © 2011-2022 走看看