From 4b1b056d76843a091247290394af7512035238c7 Mon Sep 17 00:00:00 2001 From: sotnemfresh <98352598+sotnemfresh@users.noreply.github.com> Date: Wed, 29 Apr 2026 15:03:34 +0300 Subject: [PATCH 1/3] User-Register setup --- package-lock.json | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index f07de70..f5a8977 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,6 +48,7 @@ "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.29.0.tgz", "integrity": "sha512-CGOfOJqWjg2qW/Mb6zNsDm+u5vFQ8DxXfbM09z69p5Z6+mE1ikP2jUXw+j42Pf1XTYED2Rni5f95npYeuwMDQA==", "license": "MIT", + "peer": true, "dependencies": { "@babel/code-frame": "^7.29.0", "@babel/generator": "^7.29.0", @@ -1211,6 +1212,7 @@ "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.2.tgz", "integrity": "sha512-xjIYgE8HBrkpd/sJqOGNspf8uHG+NOHGOw6a/Urj8taM2EXfdNAH2oFcPeIFfsv3+kz/mJrS5VuMqbNLjCa2vw==", "dev": true, + "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -1566,6 +1568,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "baseline-browser-mapping": "^2.9.0", "caniuse-lite": "^1.0.30001759", @@ -2131,6 +2134,7 @@ "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.35.0.tgz", "integrity": "sha512-BxAf1fVL7w+JLRQhWl2pzGeSiGqbWumV4WNvc9Rhp6tiCtm4oHnyPBSEtMGZwrQgudFQ+otqzWoPB7x+hxoWsw==", "dev": true, + "peer": true, "dependencies": { "@eslint/eslintrc": "^2.0.0", "@eslint/js": "8.35.0", @@ -5920,6 +5924,7 @@ "version": "7.29.0", "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.29.0.tgz", "integrity": "sha512-CGOfOJqWjg2qW/Mb6zNsDm+u5vFQ8DxXfbM09z69p5Z6+mE1ikP2jUXw+j42Pf1XTYED2Rni5f95npYeuwMDQA==", + "peer": true, "requires": { "@babel/code-frame": "^7.29.0", "@babel/generator": "^7.29.0", @@ -6749,7 +6754,8 @@ "version": "8.8.2", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.2.tgz", "integrity": "sha512-xjIYgE8HBrkpd/sJqOGNspf8uHG+NOHGOw6a/Urj8taM2EXfdNAH2oFcPeIFfsv3+kz/mJrS5VuMqbNLjCa2vw==", - "dev": true + "dev": true, + "peer": true }, "acorn-jsx": { "version": "5.3.2", @@ -6999,6 +7005,7 @@ "version": "4.28.1", "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.28.1.tgz", "integrity": "sha512-ZC5Bd0LgJXgwGqUknZY/vkUQ04r8NXnJZ3yYi4vDmSiZmC/pdSN0NbNRPxZpbtO4uAfDUAFffO8IZoM3Gj8IkA==", + "peer": true, "requires": { "baseline-browser-mapping": "^2.9.0", "caniuse-lite": "^1.0.30001759", @@ -7393,6 +7400,7 @@ "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.35.0.tgz", "integrity": "sha512-BxAf1fVL7w+JLRQhWl2pzGeSiGqbWumV4WNvc9Rhp6tiCtm4oHnyPBSEtMGZwrQgudFQ+otqzWoPB7x+hxoWsw==", "dev": true, + "peer": true, "requires": { "@eslint/eslintrc": "^2.0.0", "@eslint/js": "8.35.0", From dcbd643e89aa0314e1a8ef3a504334df90a27e4c Mon Sep 17 00:00:00 2001 From: sotnemfresh <98352598+sotnemfresh@users.noreply.github.com> Date: Wed, 29 Apr 2026 15:08:29 +0300 Subject: [PATCH 2/3] Lisatud Testid ning lisatud validate rida kui ei ole passwordi/username Co-authored-by: Copilot --- README.md | 6 +++ app.test.js | 87 ++++++++++++++++++++++++++++++---- validation/validatePassword.js | 1 + validation/validateUsername.js | 1 + 4 files changed, 86 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c3d6d34..e64de70 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ +## Ülesanne 1: API testid tuelmused +Test Suites: 5 passed, 5 total +Tests: 37 passed, 37 total +Snapshots: 0 total +Time: 29.56 s + # Integration testing - user registration system The task is to add tests to registration functionality. diff --git a/app.test.js b/app.test.js index f1b561d..07d1725 100644 --- a/app.test.js +++ b/app.test.js @@ -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', () => { @@ -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) + }) }) \ No newline at end of file diff --git a/validation/validatePassword.js b/validation/validatePassword.js index 44d8a8b..0f9eb84 100644 --- a/validation/validatePassword.js +++ b/validation/validatePassword.js @@ -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); diff --git a/validation/validateUsername.js b/validation/validateUsername.js index 30213b1..088ba22 100644 --- a/validation/validateUsername.js +++ b/validation/validateUsername.js @@ -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); From 60dbe33682e2f2bf976898c80048b983ee2b1e9f Mon Sep 17 00:00:00 2001 From: sotnemfresh <98352598+sotnemfresh@users.noreply.github.com> Date: Wed, 29 Apr 2026 15:21:05 +0300 Subject: [PATCH 3/3] Lisatud Mocked API testid ning tulemus kinnitatud README faili Co-authored-by: Copilot --- README.md | 6 ++++ app.mock.test.js | 87 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e64de70..fc36f47 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,12 @@ 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. diff --git a/app.mock.test.js b/app.mock.test.js index 79b9449..99d4827 100644 --- a/app.mock.test.js +++ b/app.mock.test.js @@ -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', () => { @@ -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) + }) }) \ No newline at end of file