Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Kirill Mokevnin <[email protected]>
  • Loading branch information
mokevnin committed Aug 29, 2024
1 parent 12fc222 commit a82ad2d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Node.js CI

on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '22.x'
- run: npm ci
- run: npm run build --if-present
- run: npm test
11 changes: 7 additions & 4 deletions routes/api/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ export default async function (fastify) {
schema: schema['/courses/{id}'].PATCH.args.properties,
},
async (request) => {
// fastify.assert.equal(a, b, 403)
const course = await db.update(schemas.courses)
const course = await db.query.courses.findFirst({
where: eq(schemas.courses.id, request.params.id),
})
fastify.assert(course, 404)

fastify.assert.equal(request.user.id, course?.creatorId, 403)
await db.update(schemas.courses)
.set(request.body)
.where(eq(schemas.courses.id, request.params.id))
.returning()
fastify.assert(course, 404)

return { id: request.params.id }
},
Expand Down
12 changes: 8 additions & 4 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import assert from 'assert'
import helper from 'fastify-cli/helper.js'
import path from 'path'
import { fileURLToPath } from 'url'
import * as schemas from '../db/schema.js'
import { eq } from 'drizzle-orm'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
Expand Down Expand Up @@ -49,10 +51,12 @@ async function build(t) {
}

/**
* @param {import('fastify').FastifyInstance} app
*/
async function getAuthHeader(app) {
const client = await app.db.query.users.findFirst()
* @param {import('fastify').FastifyInstance} app
* @param {number | null} userId
*/
async function getAuthHeader(app, userId = null) {
const from = app.db.select().from(schemas.users)
const [client] = userId ? await from.where(eq(schemas.users.id, userId)) : await from.limit(1)
assert.ok(client)
const token = app.jwt.sign({ id: client.id })
return {
Expand Down
2 changes: 1 addition & 1 deletion test/routes/api/courses.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test('patch courses/:id', async (t) => {
const course = await app.db.query.courses.findFirst()
assert.ok(course)

const authHeader = await getAuthHeader(app)
const authHeader = await getAuthHeader(app, course.creatorId)
const res = await app.inject({
method: 'patch',
url: `/api/courses/${course.id}`,
Expand Down

0 comments on commit a82ad2d

Please sign in to comment.