Skip to content

Instantly share code, notes, and snippets.

@marsen
Last active November 12, 2021 03:14
Show Gist options
  • Save marsen/7db60f3354d65f8c1f7cb297c6c40c35 to your computer and use it in GitHub Desktop.
Save marsen/7db60f3354d65f8c1f7cb297c6c40c35 to your computer and use it in GitHub Desktop.
TA_A13
context('# [修改使用者權限] for normal admin', () => {
before(() => {
// 製作假資料
// 本 context 會用這筆資料進行測試
this.UserMock = createModelMock('User', {
id: 1,
email: 'not_root@example.com',
name: 'admin',
isAdmin: true, // 是管理者
})
// 將 adminController 中的 User db 取代成 User mock db
this.adminController = createControllerProxy(
'../controllers/adminController',
{ User: this.UserMock }
)
})
it(' PUT /admin/users/:id/toggleAdmin ', async () => {
// 模擬 request & response
const req = mockRequest({ params: { id: 1 } }) // 帶入 params.id = 1,對 PUT /admin/users/1/toggleAdmin 發出請求
const res = mockResponse()
// 測試作業指定的 adminController.toggleAdmin 函式
await this.adminController.toggleAdmin(req, res)
req.flash.calledWith('success_messages', '使用者權限變更成功').should.be
.true
// toggleAdmin 執行完畢後,應呼叫 res.redirect 並重新導向 /admin/users
res.redirect.calledWith('/admin/users').should.be.true
// toggleAmin 執行完畢後,假資料中 id:1 使用者的應該要是 isAdmin:true
// 將假資料撈出,比對確認有成功修改到
const user = await this.UserMock.findOne({ where: { id: 1 } })
user.isAdmin.should.equal(false)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment