Skip to content
Closed
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## Ülesanne 1: API testid tuelmused
Test Suites: 5 passed, 5 total
Tests: 37 passed, 37 total
Snapshots: 0 total
Time: 29.56 s

## Ülesanne 2: Mocked API testid
Test Suites: 1 passed, 1 total
Tests: 12 passed, 12 total
Snapshots: 0 total
Time: 0.877 s

# Integration testing - user registration system
The task is to add tests to registration functionality.

Expand Down
87 changes: 78 additions & 9 deletions app.mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,32 @@ describe('given correct username and password', () => {
expect(response.body.userId).toBeDefined();
})

// test response content type?
// test response message
// test response user id value
// ...
test('returns correct message', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})
expect(response.body.message).toBe("Valid User")
})

test('content-type is json', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})
expect(response.headers['content-type']).toMatch(/json/)
})

test('userId has correct value', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})
expect(response.body.userId).toBe('1')
})
})

describe('given incorrect or missing username and password', () => {
Expand All @@ -51,9 +73,56 @@ describe('given incorrect or missing username and password', () => {
expect(response.statusCode).toBe(400)
})

// test response message
// test that response does NOT have userId
// test incorrect username or password according to requirements
// test missing username or password
// ...
test('returns error message', async () => {
const response = await request(app).post('/users').send({
username: 'user',
password: 'password',
email: 'not-an-email'
})
expect(response.body.error).toBe("Invalid User")
})

test('does NOT return userId', async () => {
const response = await request(app).post('/users').send({
username: 'user',
password: 'password',
email: 'not-an-email'
})
expect(response.body.userId).toBeUndefined()
})

test('missing username returns 400', async () => {
const response = await request(app).post('/users').send({
password: 'Password123',
email: 'student@example.com'
})
expect(response.statusCode).toBe(400)
})

test('missing password returns 400', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
email: 'student@example.com'
})
expect(response.statusCode).toBe(400)
})

test('missing email returns 400 (coverage for validateEmail)', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123'
})

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

test('email is not a string returns 400', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 12345
})

expect(response.statusCode).toBe(400)
})
})
87 changes: 78 additions & 9 deletions app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,32 @@ describe('given correct username and password', () => {
expect(response.body.userId).toBeDefined();
})

// test response content type?
// test response message
// test response user id value
// ...
test('returns correct message', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})
expect(response.body.message).toBe("Valid User")
})

test('content-type is json', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})
expect(response.headers['content-type']).toMatch(/json/)
})

test('userId has correct value', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 'student@example.com'
})
expect(response.body.userId).toBe('1')
})
})

describe('given incorrect or missing username and password', () => {
Expand All @@ -41,9 +63,56 @@ describe('given incorrect or missing username and password', () => {
expect(response.statusCode).toBe(400)
})

// test response message
// test that response does NOT have userId
// test incorrect username or password according to requirements
// test missing username or password
// ...
test('returns error message', async () => {
const response = await request(app).post('/users').send({
username: 'user',
password: 'password',
email: 'not-an-email'
})
expect(response.body.error).toBe("Invalid User")
})

test('does NOT return userId', async () => {
const response = await request(app).post('/users').send({
username: 'user',
password: 'password',
email: 'not-an-email'
})
expect(response.body.userId).toBeUndefined()
})

test('missing username returns 400', async () => {
const response = await request(app).post('/users').send({
password: 'Password123',
email: 'student@example.com'
})
expect(response.statusCode).toBe(400)
})

test('missing password returns 400', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
email: 'student@example.com'
})
expect(response.statusCode).toBe(400)
})

test('missing email returns 400 (coverage for validateEmail)', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123'
})

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

test('email is not a string returns 400', async () => {
const response = await request(app).post('/users').send({
username: 'Username',
password: 'Password123',
email: 12345
})

expect(response.statusCode).toBe(400)
})
})
10 changes: 9 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions validation/validatePassword.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function validatePassword(password) {
if (!password) return false;
const validLength = password.length >= 8;
const hasNumber = /[0-9]/g.test(password);
const hasUpperCaseLetters = /[A-Z]/g.test(password);
Expand Down
1 change: 1 addition & 0 deletions validation/validateUsername.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function validateUsername(username) {
if (!username) return false;
const validLength = username.length >= 6 && username.length <=30;
const allowedcharacters = /^[a-zA-Z0-9.]+$/g.test(username);

Expand Down
Loading