Skip to content
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

Migrate add role incl. user description bugfix and test updates #1518

Merged
merged 33 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4437f8b
refactor(addRole): move into resolver
AndreasHuber May 15, 2024
4daf5d9
fix(addRole): fix typo in SQL query
AndreasHuber May 15, 2024
e2534f9
fix(resolveUuidFromDatabase): fix for description being null
AndreasHuber May 15, 2024
b2c42e3
test(addRole): use users from database instead of mocks not working a…
AndreasHuber May 16, 2024
8ba74db
style(addRole): format
AndreasHuber May 16, 2024
7c7405e
test(addRole): make clearer to read
AndreasHuber May 16, 2024
16c124e
test(utils): make forLoginUser use a user from the database instead o…
AndreasHuber May 16, 2024
1fa34b4
test(user): fix by using user from database instead of mock
AndreasHuber May 16, 2024
e10c34d
test(user): format
AndreasHuber May 16, 2024
1572ec2
test(user): less magic number use
AndreasHuber May 17, 2024
b185cbd
test(ai): change role
AndreasHuber May 22, 2024
c79ccc5
test(authorization): replace mocked user with real one
AndreasHuber May 22, 2024
9774964
Merge branch 'staging' into migrate-add-role
AndreasHuber May 22, 2024
f0d671b
test(ai): change role
AndreasHuber May 22, 2024
c4bb8cf
test(abstract-uuid): remove tests for database-layer fail
AndreasHuber May 22, 2024
836cae3
test(abstract-uuid): format
AndreasHuber May 22, 2024
ee4d265
Merge branch 'staging' into migrate-add-role
AndreasHuber May 24, 2024
ab0968b
Merge branch 'staging' into migrate-add-role
kulla May 25, 2024
93777d6
test: Delete legacy tests
kulla May 26, 2024
75106c3
test: Add filename to sourcemaps
kulla May 26, 2024
5832804
test: Rename esbuild transformer file
kulla May 26, 2024
13bcf0b
test: Fix forLoginUser()
kulla May 26, 2024
a3638b4
test: Update abstract-uuid.ts
kulla May 26, 2024
8426f56
Merge remote-tracking branch 'origin/staging' into migrate-add-role
kulla May 26, 2024
fd6e2ea
test: Fix `user2`
kulla May 26, 2024
c869b7b
test: Fix delete-bots.ts
kulla May 26, 2024
daaeb46
fix(user): Fix parsing of SQL when roles is empty
kulla May 27, 2024
7698885
test: Fix delete-regular-users.ts
kulla May 27, 2024
dac724a
test: Fix and refactor delete-bots
kulla May 27, 2024
3555ad2
feat(addRole): create the role if it does not already exist in table
AndreasHuber May 31, 2024
28c5ba5
fix(addRole): return UserInputError if username does not belong to a …
AndreasHuber May 31, 2024
b236f1b
feat(addRole): add and use function resolveIdFromUsername
AndreasHuber May 31, 2024
fc8accd
feat(addRole): use function resolveIdFromUsername at the start simpli…
AndreasHuber May 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions __fixtures__/uuid/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const user: Model<'User'> = {
trashed: false,
alias: '/user/1/admin',
username: 'admin',
date: '2014-03-01T20:36:21Z',
date: '2014-03-01T20:36:21.000Z',
lastLogin: '2020-03-24T09:40:55Z',
description: null,
roles: ['login', 'german_horizonhelper', 'sysadmin'],
Expand All @@ -17,10 +17,9 @@ export const user2: Model<'User'> = {
__typename: DiscriminatorType.User,
id: 23,
trashed: false,
alias: '/user/23/sandra',
username: 'sandra',
date: '2015-02-01T20:35:21Z',
lastLogin: '2019-03-23T09:20:55Z',
alias: '/user/23/1229902f',
username: '1229902f',
date: '2014-03-01T20:36:32.000Z',
description: null,
roles: ['login'],
}
Expand Down
37 changes: 28 additions & 9 deletions __tests__/__utils__/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { DocumentNode } from 'graphql'
import gql from 'graphql-tag'
import * as R from 'ramda'

import { given, nextUuid } from '.'
import { user } from '../../__fixtures__'
import { Context } from '~/context'
import { Service } from '~/context/service'
import { ModelDataSource } from '~/internals/data-source'
Expand Down Expand Up @@ -98,16 +96,37 @@ export class Query<
return new Query(new Client(context), this.query)
}

forLoginUser(...additionalRoles: string[]) {
const loginUser = {
...user,
id: nextUuid(user.id),
roles: [...additionalRoles, 'login'],
async forUser(...additionalRoles: string[]) {
const userWithoutRolesId = 35478

for (const role of additionalRoles) {
const result = await databaseForTests.fetchOptional<{ id: number }>(
'select id from role where name = ?',
[role],
)

let roleId = result?.id

if (roleId == null) {
const result = await databaseForTests.mutate(
'insert into role (name) values (?)',
[role],
)

roleId = result.insertId
}

await databaseForTests.mutate(
'insert into role_user (user_id, role_id) values (?, ?)',
[userWithoutRolesId, roleId],
)
}

given('UuidQuery').for(loginUser)
return this.withContext({ userId: userWithoutRolesId })
}

return this.withContext({ userId: loginUser.id })
forLoginUser() {
return this.withContext({ userId: 9 })
}

forUnauthenticatedUser() {
Expand Down
19 changes: 19 additions & 0 deletions __tests__/__utils__/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ export const taxonomyTermQuery = new Client().prepareQuery({
`,
})

export const userQuery = new Client().prepareQuery({
query: gql`
query ($id: Int!) {
uuid(id: $id) {
id
__typename
... on User {
roles {
nodes {
role
scope
}
}
}
}
}
`,
})

export const threadsQuery = new Client().prepareQuery({
query: gql`
query thread($id: Int!, $archived: Boolean) {
Expand Down
6 changes: 4 additions & 2 deletions __tests__/schema/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ test('successfully generate content for student (not logged in) - staging', asyn
})

test('successfully generate content for architect - staging', async () => {
await query.forLoginUser('de_architect').shouldReturnData({
const newQuery = await query.forUser('de_architect')
await newQuery.shouldReturnData({
ai: {
executePrompt: {
success: true,
Expand All @@ -152,7 +153,8 @@ test('fails for unauthenticated user in production', async () => {
test('fails for unauthorized user (wrong role) in production', async () => {
const previousEnvironment = process.env.ENVIRONMENT
process.env.ENVIRONMENT = 'production'
await query.forLoginUser('de_architect').shouldFailWithError('FORBIDDEN')
const newQuery = await query.forUser('de_moderator')
await newQuery.shouldFailWithError('FORBIDDEN')
process.env.ENVIRONMENT = previousEnvironment
})

Expand Down
21 changes: 11 additions & 10 deletions __tests__/schema/authorization.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Scope, Thread } from '@serlo/authorization'
import gql from 'graphql-tag'

import { user } from '../../__fixtures__'
import { given, Client } from '../__utils__'
import { Client } from '../__utils__'
import { resolveRolesPayload, RolesPayload } from '~/schema/authorization/roles'
import { Role } from '~/types'

Expand All @@ -22,9 +21,7 @@ describe('authorization', () => {
})

test('Authenticated Users (no special roles)', async () => {
given('UuidQuery').for({ ...user, roles: ['login'] })

await new Client({ userId: user.id })
await new Client({ userId: 20 })
.prepareQuery({
query: gql`
{
Expand All @@ -38,9 +35,7 @@ describe('authorization', () => {
})

test('Authenticated Users (filter old legacy roles)', async () => {
given('UuidQuery').for({ ...user, roles: ['login', 'german_moderator'] })

await new Client({ userId: user.id })
await new Client({ userId: 33931 })
.prepareQuery({
query: gql`
{
Expand All @@ -54,9 +49,15 @@ describe('authorization', () => {
})

test('Authenticated Users (map new legacy roles)', async () => {
given('UuidQuery').for({ ...user, roles: ['login', 'de_moderator'] })
const { insertId } = await databaseForTests.mutate(
"insert into role (name) values ('de_moderator')",
)
await databaseForTests.mutate(
`insert into role_user (user_id, role_id) values (33931, ?)`,
[insertId],
)

await new Client({ userId: user.id })
await new Client({ userId: 33931 })
.prepareQuery({
query: gql`
{
Expand Down
3 changes: 2 additions & 1 deletion __tests__/schema/entity/checkout-revision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ test('fails when user is not authenticated', async () => {
})

test('fails when user does not have role "reviewer"', async () => {
await mutation.forLoginUser('de_moderator').shouldFailWithError('FORBIDDEN')
const newMutation = await mutation.forUser('de_moderator')
await newMutation.shouldFailWithError('FORBIDDEN')
})

test('fails when database layer returns a 400er response', async () => {
Expand Down
3 changes: 2 additions & 1 deletion __tests__/schema/entity/reject-revision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ test('fails when user is not authenticated', async () => {
})

test('fails when user does not have role "reviewer"', async () => {
await mutation.forLoginUser('de_moderator').shouldFailWithError('FORBIDDEN')
const newMutation = await mutation.forUser('de_moderator')
await newMutation.shouldFailWithError('FORBIDDEN')
})

test('fails when database layer returns a 400er response', async () => {
Expand Down