forked from base/web
-
Notifications
You must be signed in to change notification settings - Fork 0
Reject passwords with leading/trailing whitespace in registration #34
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
Draft
Copilot
wants to merge
2
commits into
master
Choose a base branch
from
copilot/fix-user-authentication-issue
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,333 @@ | ||
| /** | ||
| * @jest-environment node | ||
| */ | ||
| import { NextRequest } from 'next/server'; | ||
| import { POST } from './route'; | ||
|
|
||
| // Helper function to create a mock NextRequest | ||
| function createMockRequest(body: unknown): NextRequest { | ||
| const request = new NextRequest('http://localhost:3000/api/auth/register', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify(body), | ||
| }); | ||
| return request; | ||
| } | ||
|
|
||
| describe('POST /api/auth/register', () => { | ||
| describe('Successful registration', () => { | ||
| it('should register a user with valid data', async () => { | ||
| const validData = { | ||
| username: 'testuser', | ||
| email: 'test@example.com', | ||
| password: 'password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(validData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(201); | ||
| expect(data).toEqual({ | ||
| message: 'Registration successful', | ||
| user: { | ||
| username: 'testuser', | ||
| email: 'test@example.com', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should trim username and email whitespace', async () => { | ||
| const dataWithWhitespace = { | ||
| username: ' testuser ', | ||
| email: ' test@example.com ', | ||
| password: 'password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(dataWithWhitespace); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(201); | ||
| expect(data.user.username).toBe('testuser'); | ||
| expect(data.user.email).toBe('test@example.com'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Password validation', () => { | ||
| it('should reject password with leading whitespace', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| email: 'test@example.com', | ||
| password: ' password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Password cannot have leading or trailing spaces'); | ||
| }); | ||
|
|
||
| it('should reject password with trailing whitespace', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| email: 'test@example.com', | ||
| password: 'password123 ', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Password cannot have leading or trailing spaces'); | ||
| }); | ||
|
|
||
| it('should reject password with both leading and trailing whitespace', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| email: 'test@example.com', | ||
| password: ' password123 ', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Password cannot have leading or trailing spaces'); | ||
| }); | ||
|
|
||
| it('should allow password with spaces in the middle', async () => { | ||
| const validData = { | ||
| username: 'testuser', | ||
| email: 'test@example.com', | ||
| password: 'pass word 123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(validData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(201); | ||
| expect(data.message).toBe('Registration successful'); | ||
| }); | ||
|
|
||
| it('should reject password shorter than 8 characters', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| email: 'test@example.com', | ||
| password: 'pass123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Password must be at least 8 characters'); | ||
| }); | ||
|
|
||
| it('should reject empty password', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| email: 'test@example.com', | ||
| password: '', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Missing required fields'); | ||
| }); | ||
|
|
||
| it('should reject non-string password', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| email: 'test@example.com', | ||
| password: 12345678, | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Password must be at least 8 characters'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Username validation', () => { | ||
| it('should reject username shorter than 3 characters', async () => { | ||
| const invalidData = { | ||
| username: 'ab', | ||
| email: 'test@example.com', | ||
| password: 'password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Username must be at least 3 characters'); | ||
| }); | ||
|
|
||
| it('should reject empty username', async () => { | ||
| const invalidData = { | ||
| username: '', | ||
| email: 'test@example.com', | ||
| password: 'password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Missing required fields'); | ||
| }); | ||
|
|
||
| it('should reject username with only whitespace', async () => { | ||
| const invalidData = { | ||
| username: ' ', | ||
| email: 'test@example.com', | ||
| password: 'password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Missing required fields'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Email validation', () => { | ||
| it('should reject invalid email format', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| email: 'invalid-email', | ||
| password: 'password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Invalid email format'); | ||
| }); | ||
|
|
||
| it('should reject email without @', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| email: 'testexample.com', | ||
| password: 'password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Invalid email format'); | ||
| }); | ||
|
|
||
| it('should reject email without domain', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| email: 'test@', | ||
| password: 'password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Invalid email format'); | ||
| }); | ||
|
|
||
| it('should reject empty email', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| email: '', | ||
| password: 'password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Missing required fields'); | ||
| }); | ||
|
|
||
| it('should reject email with only whitespace', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| email: ' ', | ||
| password: 'password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Missing required fields'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Missing fields', () => { | ||
| it('should reject request with missing username', async () => { | ||
| const invalidData = { | ||
| email: 'test@example.com', | ||
| password: 'password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Missing required fields'); | ||
| }); | ||
|
|
||
| it('should reject request with missing email', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| password: 'password123', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Missing required fields'); | ||
| }); | ||
|
|
||
| it('should reject request with missing password', async () => { | ||
| const invalidData = { | ||
| username: 'testuser', | ||
| email: 'test@example.com', | ||
| }; | ||
|
|
||
| const request = createMockRequest(invalidData); | ||
| const response = await POST(request); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(data.error).toBe('Missing required fields'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / Bearer
Observable Timing Discrepancy Error