Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/server/src/api/challs/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { makeFastifyRoute } from '../helpers'
import config from '../../config/server'
import { getCleanedChallenges } from '../../challenges'
import { getChallengeInfo } from '../../cache/leaderboard'
import Permissions from '../../util/perms'

export default makeFastifyRoute(challsGet, async ({ res }) => {
if (Date.now() < config.startTime) {
export default makeFastifyRoute(challsGet, async ({ res, user }) => {
if (Date.now() < config.startTime && !(user.perms & Permissions.challsRead)) {
return res.badNotStarted()
}

Expand Down
10 changes: 6 additions & 4 deletions packages/server/test/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import * as db from '../src/database'
import { Challenge } from '../src/challenges/types'

// Generate only valid parameters
export const generateTestUser = (): Omit<db.users.User, 'id'> => ({
export const generateTestUser = (perms = 0): Omit<db.users.User, 'id'> => ({
email: uuidv4() + '@test.com',
name: uuidv4(),
division: Object.keys(config.divisions)[0],
perms: 0,
perms: perms,
})

// Generate a real user, adding to database
export const generateRealTestUser = async (): Promise<{
export const generateRealTestUser = async (
perms = 0
): Promise<{
user: db.users.User
cleanup: () => Promise<void>
}> => {
const id = uuidv4()

const userData = generateTestUser()
const userData = generateTestUser(perms)
const user = await db.users.makeUser({
...userData,
id,
Expand Down
25 changes: 24 additions & 1 deletion packages/server/test/integration/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {
badNotStarted,
goodChallenges,
} from '@rctf/api-types/responses'
import Permissions from '../../src/util/perms'

let uuid, testUserData
let uuid, testUserData, adminUuid, testAdminUserData

beforeAll(async () => {
await app.ready()
Expand All @@ -18,6 +19,9 @@ beforeAll(async () => {
beforeAll(async () => {
testUserData = await util.generateRealTestUser()
uuid = testUserData.user.id

testAdminUserData = await util.generateRealTestUser(Permissions.challsRead)
adminUuid = testAdminUserData.user.id
})

afterAll(async () => {
Expand Down Expand Up @@ -58,3 +62,22 @@ test('succeeds with goodChallenges', async () => {
expect(resp.body.kind).toBe('goodChallenges')
expect(Array.isArray(resp.body.data)).toBe(true)
})

test('succeds with goodChallenges for admin', async () => {
const oldTime = config.startTime
// Choose a time 10 minutes in the future
config.startTime = Date.now() + 10 * 60 * 1000

const authToken = await auth.token.getToken(
auth.token.tokenKinds.auth,
adminUuid
)
const resp = await request(app.server)
.get(process.env.API_ENDPOINT + '/challs')
.set('Authorization', ' Bearer ' + authToken)
.expect(goodChallenges.status)

expect(resp.body.kind).toBe('goodChallenges')

config.startTime = oldTime
})
Loading