Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation is added or a request the has been made by the superadmin #1097

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/consts/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const enums = {
SPOKEN_LANG_ENUM: ['English', 'Ukrainian', 'Polish', 'German', 'French', 'Spanish', 'Arabic'],
PROFICIENCY_LEVEL_ENUM: ['Beginner', 'Intermediate', 'Advanced', 'Test Preparation', 'Professional', 'Specialized'],
ROLE_ENUM: ['student', 'tutor', 'admin', 'superadmin'],
LOGIN_ROLE_ENUM: ['student', 'tutor', 'admin'],
LOGIN_ROLE_ENUM: ['student', 'tutor', 'admin', 'superadmin'],
MAIN_ROLE_ENUM: ['student', 'tutor'],
STATUS_ENUM: ['active', 'blocked', 'deactivated'],
COOPERATION_STATUS_ENUM: ['pending', 'active', 'declined', 'closed', 'request to close'],
Expand Down
6 changes: 6 additions & 0 deletions src/routes/adminInvitation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ const router = require('express').Router()

const langMiddleware = require('~/middlewares/appLanguage')
const asyncWrapper = require('~/middlewares/asyncWrapper')
const { authMiddleware, restrictTo } = require('~/middlewares/auth')
const {
roles: { SUPERADMIN }
} = require('~/consts/auth')

const adminInvitationController = require('~/controllers/adminInvitation')
router.use(authMiddleware)

router.use(restrictTo(SUPERADMIN))
router.post('/', langMiddleware, asyncWrapper(adminInvitationController.sendAdminInvitations))
router.get('/', asyncWrapper(adminInvitationController.getAdminInvitations))

Expand Down
83 changes: 66 additions & 17 deletions src/test/integration/controllers/adminInvitation.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
const { serverInit, serverCleanup, stopServer } = require('~/test/setup')
const testUserAuthentication = require('~/utils/testUserAuth')

const emails = ['[email protected]', '[email protected]']

let studentUser = {
role: ['student'],
firstName: 'TestStudent',
lastName: 'StudentTest',
email: '[email protected]',
password: 'studentpassword123',
appLanguage: 'en',
isEmailConfirmed: true,
isFirstLogin: false,
lastLoginAs: 'student'
}

let superadminUser = {
role: ['superadmin'],
firstName: 'TestAdmin',
lastName: 'AdminTest',
email: '[email protected]',
password: 'supersecretpass123',
appLanguage: 'en',
isEmailConfirmed: true,
isFirstLogin: false,
lastLoginAs: 'superadmin'
}

const endpointURL = '/admin-invitations'

describe('Admin invitation controller', () => {
let app, server, response
beforeAll(async () => {
; ({ app, server } = await serverInit())
})
let app, server, response, accessToken

beforeEach(async () => {
response = await app.post(endpointURL).send({ emails }).set('Accept-Language', 'en')
beforeAll(async () => {
;({ app, server } = await serverInit())
})

afterEach(async () => {
Expand All @@ -23,24 +46,50 @@ describe('Admin invitation controller', () => {

describe('admin-invitations endpoint', () => {
describe(`POST ${endpointURL}`, () => {
it('should send admin invitations', async () => {
expect(response.statusCode).toBe(201)
it('should throw FORBIDDEN (student user)', async () => {
accessToken = await testUserAuthentication(app, studentUser)

expect(response.body).toEqual(
expect.arrayContaining([
expect.objectContaining({ email: emails[0] }),
expect.objectContaining({ email: emails[1] })
])
)
response = await app
.post(endpointURL)
.send({ emails })
.set('Accept-Language', 'en')
.set('Cookie', `accessToken=${accessToken}`)

expect(response.statusCode).toBe(403)
})

it('should send admin invitations (superadmin user)', async () => {
accessToken = await testUserAuthentication(app, superadminUser)

response = await app
.post(endpointURL)
.send({ emails })
.set('Accept-Language', 'en')
.set('Cookie', `accessToken=${accessToken}`)

expect(response.statusCode).toBe(201)
expect.arrayContaining([
expect.objectContaining({ email: emails[0] }),
expect.objectContaining({ email: emails[1] })
])
})
})

describe(`GET ${endpointURL}`, () => {
it('should get admin invitations', async () => {
const { body, statusCode } = await app.get(endpointURL)
it('should get admin invitations (superadmin user)', async () => {
accessToken = await testUserAuthentication(app, superadminUser)
await app
.post(endpointURL)
.send({ emails })
.set('Accept-Language', 'en')
.set('Cookie', `accessToken=${accessToken}`)

expect(statusCode).toBe(200)
const { statusCode, body } = await app
.get(endpointURL)
.set('Accept-Language', 'en')
.set('Cookie', `accessToken=${accessToken}`)

expect(statusCode).toBe(200)
expect(body).toEqual(
expect.arrayContaining([
expect.objectContaining({ email: emails[0] }),
Expand Down
Loading