-
Notifications
You must be signed in to change notification settings - Fork 42
Test: add vitest tests for GET and PUT /api/profiles/me routes #76
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
Open
Dipti45sktech
wants to merge
2
commits into
Dev-Card:main
Choose a base branch
from
Dipti45sktech:profile
base: main
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.
Open
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 |
|---|---|---|
| @@ -1,39 +1,103 @@ | ||
| import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
| import Fastify from 'fastify'; | ||
| import { profileRoutes } from '../routes/profiles.js'; | ||
|
|
||
| // Mock test for duplicate username check | ||
| // Note: This test verifies the expected behavior of the /api/profiles/me PUT endpoint | ||
| // when attempting to change username to one that's already taken. | ||
| // | ||
| // The actual implementation in profiles.ts (lines 54-63) already handles this correctly: | ||
| // - Checks for existing username with different user ID | ||
| // - Returns 409 status with { error: "Username already taken" } | ||
| // | ||
| // Concurrency note: The current implementation uses a simple findFirst query. | ||
| // For production, consider adding a timestamp/version field to handle race conditions | ||
| // where two users might try to claim the same username simultaneously. | ||
|
|
||
| describe('PUT /api/profiles/me - Duplicate Username', () => { | ||
| // This test would require setting up the full Fastify app with test database | ||
| // For now, documenting the expected behavior based on profiles.ts implementation | ||
|
|
||
| it('should return 409 with error "Username already taken" when username exists', async () => { | ||
| // Expected behavior (from profiles.ts lines 54-63): | ||
| // const existing = await app.prisma.user.findFirst({ | ||
| // where: { | ||
| // username: parsed.data.username, | ||
| // NOT: { id: userId }, | ||
| // }, | ||
| // }); | ||
| // if (existing) { | ||
| // return reply.status(409).send({ error: 'Username already taken' }); | ||
| // } | ||
|
|
||
| // Expected response: | ||
| expect(true).toBe(true); // Placeholder - actual test needs full app setup | ||
| const mockUser = { | ||
| id: 'user-123', | ||
| email: 'test@example.com', | ||
| username: 'testuser', | ||
| displayName: 'Test User', | ||
| bio: null, | ||
| pronouns: null, | ||
| role: null, | ||
| company: null, | ||
| avatarUrl: null, | ||
| accentColor: '#ffffff', | ||
| platformLinks: [], | ||
| cards: [], | ||
| provider: 'github', | ||
| providerId: 'gh-123', | ||
| }; | ||
|
|
||
| const mockPrisma = { | ||
| user: { | ||
| findUnique: vi.fn(), | ||
| findFirst: vi.fn(), | ||
| update: vi.fn(), | ||
| }, | ||
| }; | ||
|
|
||
| async function buildApp() { | ||
| const app = Fastify(); | ||
| app.decorate('prisma', mockPrisma); | ||
| app.decorate('authenticate', async (request: any) => { | ||
| request.user = { id: 'user-123' }; | ||
| }); | ||
| app.register(profileRoutes, { prefix: '/api/profiles' }); | ||
| await app.ready(); | ||
| return app; | ||
| } | ||
|
|
||
| describe('GET /api/profiles/me', () => { | ||
| beforeEach(() => vi.clearAllMocks()); | ||
|
|
||
| it('should return user profile with displayName', async () => { | ||
| mockPrisma.user.findUnique.mockResolvedValue(mockUser); | ||
| const app = await buildApp(); | ||
| const res = await app.inject({ method: 'GET', url: '/api/profiles/me' }); | ||
| expect(res.statusCode).toBe(200); | ||
| const body = res.json(); | ||
| expect(body.displayName).toBe('Test User'); | ||
| expect(body.email).toBe('test@example.com'); | ||
| expect(body.provider).toBeUndefined(); | ||
| expect(body.providerId).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return 404 if user not found', async () => { | ||
| mockPrisma.user.findUnique.mockResolvedValue(null); | ||
| const app = await buildApp(); | ||
| const res = await app.inject({ method: 'GET', url: '/api/profiles/me' }); | ||
| expect(res.statusCode).toBe(404); | ||
| expect(res.json().error).toBe('User not found'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('PUT /api/profiles/me', () => { | ||
| beforeEach(() => vi.clearAllMocks()); | ||
|
|
||
| it('should update profile and return updated data', async () => { | ||
| mockPrisma.user.findFirst.mockResolvedValue(null); | ||
| mockPrisma.user.update.mockResolvedValue({ ...mockUser, displayName: 'Updated Name' }); | ||
| const app = await buildApp(); | ||
| const res = await app.inject({ | ||
| method: 'PUT', | ||
| url: '/api/profiles/me', | ||
| payload: { displayName: 'Updated Name' }, | ||
| }); | ||
| expect(res.statusCode).toBe(200); | ||
| expect(res.json().displayName).toBe('Updated Name'); | ||
| }); | ||
|
|
||
| it('should return 400 for invalid accentColor', async () => { | ||
| const app = await buildApp(); | ||
| const res = await app.inject({ | ||
| method: 'PUT', | ||
| url: '/api/profiles/me', | ||
| payload: { accentColor: 'notacolor' }, | ||
| }); | ||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Validation failed'); | ||
| }); | ||
|
|
||
| it('should allow username change when username is available', async () => { | ||
| // Expected: 200 OK with updated profile | ||
| expect(true).toBe(true); | ||
| it('should return 409 if username is already taken', async () => { | ||
| mockPrisma.user.findFirst.mockResolvedValue({ id: 'other-user' }); | ||
| const app = await buildApp(); | ||
| const res = await app.inject({ | ||
| method: 'PUT', | ||
| url: '/api/profiles/me', | ||
| payload: { username: 'takenuser' }, | ||
| }); | ||
| expect(res.statusCode).toBe(409); | ||
| expect(res.json().error).toBe('Username already taken'); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.