-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix up the tests and user creation api behavior
- Loading branch information
Showing
14 changed files
with
770 additions
and
30 deletions.
There are no files selected for viewing
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,60 @@ | ||
.env | ||
|
||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules | ||
jspm_packages | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# 0x | ||
profile-* | ||
|
||
# mac files | ||
.DS_Store | ||
|
||
# vim swap files | ||
*.swp | ||
|
||
# webstorm | ||
.idea | ||
|
||
# vscode | ||
.vscode | ||
*code-workspace | ||
|
||
# clinic | ||
profile* | ||
*clinic* | ||
*flamegraph* |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
8 changes: 8 additions & 0 deletions
8
api/prisma/migrations/20241129051559_make_user_name_unique/migration.sql
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,8 @@ | ||
/* | ||
Warnings: | ||
- A unique constraint covering the columns `[name]` on the table `User` will be added. If there are existing duplicate values, this will fail. | ||
*/ | ||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_name_key" ON "User"("name"); |
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
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
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
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
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,104 @@ | ||
// test/api/user.test.ts | ||
import Fastify, { FastifyInstance } from 'fastify'; | ||
import sensible from '@fastify/sensible'; | ||
import rootRoutes from '../../../src/routes'; | ||
import errorHandler from '../../../src/plugins/errorHandler'; | ||
|
||
describe('User API Routes', () => { | ||
let app: FastifyInstance; | ||
|
||
beforeAll(async () => { | ||
app = Fastify(); | ||
await app.register(sensible); | ||
|
||
// Register routes with API prefix and error handler | ||
await app.register( | ||
async (fastify) => { | ||
await fastify.register(errorHandler); | ||
await fastify.register(rootRoutes); | ||
}, | ||
{ prefix: '/api' }, | ||
); | ||
|
||
await app.ready(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
describe('POST /api/user', () => { | ||
const validUser = { | ||
name: 'john_doe', | ||
email: '[email protected]', | ||
password: 'password123', | ||
}; | ||
|
||
// TODO: Add test for valid user creation | ||
|
||
test('should reject user with conflicting name', async () => { | ||
// Try to create another user with the same name | ||
const response = await app.inject({ | ||
method: 'POST', | ||
url: '/api/user', | ||
payload: validUser, | ||
}); | ||
|
||
expect(response.statusCode).toBe(409); | ||
expect(response.json()).toMatchObject({ | ||
error: 'User with this name already exists', | ||
}); | ||
}); | ||
|
||
test('should reject invalid name format', async () => { | ||
const response = await app.inject({ | ||
method: 'POST', | ||
url: '/api/user', | ||
payload: { | ||
...validUser, | ||
name: 'invalid@name', | ||
}, | ||
}); | ||
|
||
expect(response.statusCode).toBe(400); | ||
expect(response.json()).toMatchObject({ | ||
error: 'Validation Error', | ||
details: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
message: 'name must contain only letters, numbers, and underscores', | ||
}), | ||
]), | ||
}); | ||
}); | ||
|
||
test('should reject missing required fields', async () => { | ||
const response = await app.inject({ | ||
method: 'POST', | ||
url: '/api/user', | ||
payload: {}, | ||
}); | ||
|
||
expect(response.statusCode).toBe(400); | ||
expect(response.json()).toMatchObject({ | ||
error: 'Validation Error', | ||
details: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
message: 'name is required', | ||
}), | ||
]), | ||
}); | ||
}); | ||
}); | ||
|
||
describe('GET /api/user', () => { | ||
test('should return list of user names', async () => { | ||
const response = await app.inject({ | ||
method: 'GET', | ||
url: '/api/user', | ||
}); | ||
|
||
expect(response.statusCode).toBe(200); | ||
expect(Array.isArray(response.json())).toBe(true); | ||
}); | ||
}); | ||
}); |
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.