Skip to content

Commit a82ad2d

Browse files
committed
update
Signed-off-by: Kirill Mokevnin <[email protected]>
1 parent 12fc222 commit a82ad2d

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

.github/workflows/push.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Node.js CI
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v4
12+
- name: Use Node.js
13+
uses: actions/setup-node@v4
14+
with:
15+
node-version: '22.x'
16+
- run: npm ci
17+
- run: npm run build --if-present
18+
- run: npm test

routes/api/courses.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,15 @@ export default async function (fastify) {
7373
schema: schema['/courses/{id}'].PATCH.args.properties,
7474
},
7575
async (request) => {
76-
// fastify.assert.equal(a, b, 403)
77-
const course = await db.update(schemas.courses)
76+
const course = await db.query.courses.findFirst({
77+
where: eq(schemas.courses.id, request.params.id),
78+
})
79+
fastify.assert(course, 404)
80+
81+
fastify.assert.equal(request.user.id, course?.creatorId, 403)
82+
await db.update(schemas.courses)
7883
.set(request.body)
7984
.where(eq(schemas.courses.id, request.params.id))
80-
.returning()
81-
fastify.assert(course, 404)
8285

8386
return { id: request.params.id }
8487
},

test/helper.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import assert from 'assert'
55
import helper from 'fastify-cli/helper.js'
66
import path from 'path'
77
import { fileURLToPath } from 'url'
8+
import * as schemas from '../db/schema.js'
9+
import { eq } from 'drizzle-orm'
810

911
const __filename = fileURLToPath(import.meta.url)
1012
const __dirname = path.dirname(__filename)
@@ -49,10 +51,12 @@ async function build(t) {
4951
}
5052

5153
/**
52-
* @param {import('fastify').FastifyInstance} app
53-
*/
54-
async function getAuthHeader(app) {
55-
const client = await app.db.query.users.findFirst()
54+
* @param {import('fastify').FastifyInstance} app
55+
* @param {number | null} userId
56+
*/
57+
async function getAuthHeader(app, userId = null) {
58+
const from = app.db.select().from(schemas.users)
59+
const [client] = userId ? await from.where(eq(schemas.users.id, userId)) : await from.limit(1)
5660
assert.ok(client)
5761
const token = app.jwt.sign({ id: client.id })
5862
return {

test/routes/api/courses.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test('patch courses/:id', async (t) => {
4747
const course = await app.db.query.courses.findFirst()
4848
assert.ok(course)
4949

50-
const authHeader = await getAuthHeader(app)
50+
const authHeader = await getAuthHeader(app, course.creatorId)
5151
const res = await app.inject({
5252
method: 'patch',
5353
url: `/api/courses/${course.id}`,

0 commit comments

Comments
 (0)