-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backend integration tests for PUT /users/:id/template
- Loading branch information
1 parent
16158ce
commit 010006e
Showing
1 changed file
with
188 additions
and
0 deletions.
There are no files selected for viewing
188 changes: 188 additions & 0 deletions
188
packages/server/src/tests/integration/users/update-post-template.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
import request from 'supertest'; | ||
import { pick } from 'lodash-es'; | ||
import { | ||
POST_SECTIONS_MAX, | ||
POST_TITLE_MAX_LENGTH, | ||
POST_MAX_TAGS, | ||
POST_MAX_TAG_LEN, | ||
} from '../../../constants/index.js'; | ||
import { signUpRequest } from '../../utils/request-auth.js'; | ||
import User from '../../../models/User.js'; | ||
import { generateRandomPost } from '../../data-generators/index.js'; | ||
import { ERRORS } from '../../../errors/index.js'; | ||
|
||
const post = generateRandomPost(); | ||
|
||
const requiredPostFields = pick(post, ['title', 'sections']); | ||
|
||
describe('PUT /users/:id/template', () => { | ||
it('Should return status 401 and an expected message if user is not signed in', async () => { | ||
const response = await request(global.app).put('/api/users/1234/template'); | ||
|
||
expect(response.body.error.message).toBe(ERRORS.UNAUTHORIZED); | ||
expect(response.status).toBe(401); | ||
}); | ||
|
||
it('Should return status 422 and an expected message if title is too long', async () => { | ||
const { sessionCookie } = await signUpRequest(global.app); | ||
|
||
const response = await request(global.app) | ||
.put('/api/users/1234/template') | ||
.set('Cookie', sessionCookie) | ||
.send({ | ||
...requiredPostFields, | ||
title: 'a'.repeat(POST_TITLE_MAX_LENGTH + 1), | ||
}); | ||
|
||
expect(response.body.error.message).toBe( | ||
ERRORS.POST_TITLE_MAX_LENGTH_EXCEEDED, | ||
); | ||
expect(response.status).toBe(422); | ||
}); | ||
|
||
it('Should return status 422 and an expected message if too many tags', async () => { | ||
const { sessionCookie } = await signUpRequest(global.app); | ||
|
||
const response = await request(global.app) | ||
.put('/api/users/1234/template') | ||
.set('Cookie', sessionCookie) | ||
.send({ | ||
...requiredPostFields, | ||
tags: Array(POST_MAX_TAGS + 1).fill(post.tags[0]), | ||
}); | ||
|
||
expect(response.body.error.message).toBe(ERRORS.POST_MAX_TAGS_EXCEEDED); | ||
expect(response.status).toBe(422); | ||
}); | ||
|
||
it('Should return status 422 and an expected message if too long tag', async () => { | ||
const { sessionCookie } = await signUpRequest(global.app); | ||
|
||
const response = await request(global.app) | ||
.put('/api/users/1234/template') | ||
.set('Cookie', sessionCookie) | ||
.send({ | ||
...requiredPostFields, | ||
tags: ['a'.repeat(POST_MAX_TAG_LEN + 1)], | ||
}); | ||
|
||
expect(response.body.error.message).toBe(ERRORS.POST_TAG_MAX_LEN_EXCEEDED); | ||
expect(response.status).toBe(422); | ||
}); | ||
|
||
it('Should return status 422 and an expected message if too many sections', async () => { | ||
const { sessionCookie } = await signUpRequest(global.app); | ||
|
||
const response = await request(global.app) | ||
.put('/api/users/1234/template') | ||
.set('Cookie', sessionCookie) | ||
.send({ | ||
...requiredPostFields, | ||
sections: Array(POST_SECTIONS_MAX + 1).fill( | ||
requiredPostFields.sections[0], | ||
), | ||
}); | ||
|
||
expect(response.body.error.message).toBe(ERRORS.POST_SECTIONS_MAX_EXCEEDED); | ||
expect(response.status).toBe(422); | ||
}); | ||
|
||
it('Should return status 200 and the updated template after a successful update', async () => { | ||
const { sessionCookie } = await signUpRequest(global.app); | ||
|
||
const response = await request(global.app) | ||
.put('/api/users/1234/template') | ||
.set('Cookie', sessionCookie) | ||
.send({ | ||
...requiredPostFields, | ||
tags: post.tags, | ||
}); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual({ | ||
title: requiredPostFields.title, | ||
tags: post.tags, | ||
sections: requiredPostFields.sections, | ||
}); | ||
}); | ||
|
||
it('Should update title in user.template in the database', async () => { | ||
const { sessionCookie, currentUser } = await signUpRequest(global.app); | ||
|
||
const template = { | ||
title: 'new title', | ||
}; | ||
|
||
await request(global.app) | ||
.put('/api/users/1234/template') | ||
.set('Cookie', sessionCookie) | ||
.send(template); | ||
|
||
const userFromDb = await User.findById(currentUser.id).lean(); | ||
|
||
expect(userFromDb.template.title).toBe(template.title); | ||
}); | ||
|
||
it('Should update tags in user.template in the database', async () => { | ||
const { sessionCookie, currentUser } = await signUpRequest(global.app); | ||
|
||
const template = { | ||
tags: ['new tag'], | ||
}; | ||
|
||
await request(global.app) | ||
.put('/api/users/1234/template') | ||
.set('Cookie', sessionCookie) | ||
.send(template); | ||
|
||
const userFromDb = await User.findById(currentUser.id).lean(); | ||
|
||
expect(userFromDb.template.tags).toEqual(template.tags); | ||
}); | ||
|
||
it('Should update sections in user.template in the database', async () => { | ||
const { sessionCookie, currentUser } = await signUpRequest(global.app); | ||
|
||
const template = { | ||
sections: [ | ||
{ | ||
type: 'text', | ||
content: 'new section', | ||
}, | ||
], | ||
}; | ||
|
||
await request(global.app) | ||
.put('/api/users/1234/template') | ||
.set('Cookie', sessionCookie) | ||
.send(template); | ||
|
||
const userFromDb = await User.findById(currentUser.id).lean(); | ||
|
||
expect(userFromDb.template.sections).toEqual(template.sections); | ||
}); | ||
|
||
it('Should update all: title, tags and sections in user.template in the database', async () => { | ||
const { sessionCookie, currentUser } = await signUpRequest(global.app); | ||
|
||
const template = { | ||
title: 'new title', | ||
tags: ['new tag'], | ||
sections: [ | ||
{ | ||
type: 'text', | ||
content: 'new section', | ||
}, | ||
], | ||
}; | ||
|
||
await request(global.app) | ||
.put('/api/users/1234/template') | ||
.set('Cookie', sessionCookie) | ||
.send(template); | ||
|
||
const userFromDb = await User.findById(currentUser.id).lean(); | ||
|
||
expect(userFromDb.template).toEqual(template); | ||
}); | ||
}); |