Skip to content

Commit

Permalink
Merge pull request #15 from pubkeyapp/beeman/sample-data
Browse files Browse the repository at this point in the history
chore: clean up sample data to optimize development flow
  • Loading branch information
beeman authored Mar 8, 2024
2 parents 3d7514e + e752079 commit 3c9195b
Show file tree
Hide file tree
Showing 86 changed files with 567 additions and 1,512 deletions.
3 changes: 0 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ API_URL=http://localhost:3000/api
#AUTH_DISCORD_CLIENT_SECRET=
# Enable login with Discord
#AUTH_DISCORD_ENABLED=true
AUTH_PASSWORD_ENABLED=true
# Enable user registration
AUTH_REGISTER_ENABLED=true
# Solana accounts that get the Admin role (comma-separated)
AUTH_SOLANA_ADMIN_IDS=
# Enable link with Solana
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ on:
pull_request:

env:
AUTH_PASSWORD_ENABLED: true
AUTH_REGISTER_ENABLED: true
AUTH_SOLANA_LOGIN_ENABLED: true
AUTH_SOLANA_REGISTER_ENABLED: true
API_URL: http://localhost:3000/api
CLOAK_KEYCHAIN: 'v1.aesgcm256.c3d07a38.BoM0uySfyLZLqg8B.in_ID0d7vrwBdUjjGwvBe0CG'
CLOAK_MASTER_KEY: 'k1.aesgcm256.vG3cv8JCPJPHaEKmcDJMA2PeXDjoVlXCC9KLu96R_Rg='
Expand Down
20 changes: 0 additions & 20 deletions api-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ input AdminCreateSnapshotInput {
roleId: String!
}

input AdminCreateUserInput {
password: String
username: String!
}

input AdminFindManyBotInput {
communityId: String!
limit: Int = 10
Expand Down Expand Up @@ -195,8 +190,6 @@ input AdminUpdateUserInput {
type AppConfig {
authLinkProviders: [IdentityProvider!]
authLoginProviders: [IdentityProvider!]
authPasswordEnabled: Boolean!
authRegisterEnabled: Boolean!
}

type Bot {
Expand Down Expand Up @@ -417,11 +410,6 @@ enum LogRelatedType {
User
}

input LoginInput {
password: String!
username: String!
}

type Mutation {
adminCreateBackup: Boolean!
adminCreateBot(input: AdminCreateBotInput!): Bot
Expand All @@ -432,7 +420,6 @@ type Mutation {
adminCreateNetworkToken(input: AdminCreateNetworkTokenInput!): NetworkToken
adminCreateRole(input: AdminCreateRoleInput!): Role
adminCreateSnapshot(input: AdminCreateSnapshotInput!): Snapshot
adminCreateUser(input: AdminCreateUserInput!): User
adminDeleteBackup(name: String!): Boolean!
adminDeleteBot(botId: String!): Boolean
adminDeleteCommunity(communityId: String!): Boolean
Expand All @@ -457,9 +444,7 @@ type Mutation {
adminUpdateRole(input: AdminUpdateRoleInput!, roleId: String!): Role
adminUpdateUser(input: AdminUpdateUserInput!, userId: String!): User
anonVerifyIdentityChallenge(input: VerifyIdentityChallengeInput!): IdentityChallenge
login(input: LoginInput!): User
logout: Boolean
register(input: RegisterInput!): User
userCreateBot(input: UserCreateBotInput!): Bot
userCreateCommunity(input: UserCreateCommunityInput!): Community
userCreateCommunityMember(communityId: String!, input: UserCreateCommunityMemberInput!): CommunityMember
Expand Down Expand Up @@ -654,11 +639,6 @@ type Query {
userRequestIdentityChallenge(input: RequestIdentityChallengeInput!): IdentityChallenge
}

input RegisterInput {
password: String!
username: String!
}

input RequestIdentityChallengeInput {
provider: IdentityProvider!
providerId: String!
Expand Down
138 changes: 31 additions & 107 deletions apps/api-e2e/src/api/api-auth-feature.spec.ts
Original file line number Diff line number Diff line change
@@ -1,114 +1,38 @@
import { RegisterInput, UserRole, UserStatus } from '@pubkey-link/sdk'
import { sdk, uniqueId } from '../support'
import { UserRole, UserStatus } from '@pubkey-link/sdk'
import { alice, bob } from '../fixtures'

import { getUserCookie } from '../support'

describe('api-auth-feature', () => {
describe('api-auth-resolver', () => {
describe('authorized', () => {
it('should log in as alice', async () => {
const res = await sdk.login({
input: { username: 'alice', password: 'password' },
})

expect(res.data.login.id).toBe('alice')
expect(res.data.login.username).toBe('alice')
expect(res.data.login.role).toBe(UserRole.Admin)
expect(res.data.login.status).toBe(UserStatus.Active)
expect((res.data.login as { password?: string }).password).toBeUndefined()

const meRes = await sdk.me(
{},
{
cookie: res.headers.get('set-cookie'),
},
)
expect(meRes.data.me.id).toBe('alice')
expect(meRes.data.me.username).toBe('alice')
expect((meRes.data.me as { password?: string }).password).toBeUndefined()
})

it('should log in as bob', async () => {
const res = await sdk.login({
input: { username: 'bob', password: 'password' },
})

expect(res.data.login.id).toBe('bob')
expect(res.data.login.username).toBe('bob')
expect(res.data.login.role).toBe(UserRole.User)
expect(res.data.login.status).toBe(UserStatus.Active)
expect((res.data.login as { password?: string }).password).toBeUndefined()

const meRes = await sdk.me(
{},
{
cookie: res.headers.get('set-cookie'),
},
)
expect(meRes.data.me.id).toBe('bob')
expect(meRes.data.me.username).toBe('bob')
expect((meRes.data.me as { password?: string }).password).toBeUndefined()
})

it('should register a new user and log in', async () => {
const input: RegisterInput = { username: uniqueId('user'), password: 'password' }
const res = await sdk.register({ input })

expect(res.data.register.id).toBeDefined()
expect(res.data.register.username).toBe(input.username)
expect(res.data.register.role).toBe(UserRole.User)
expect(res.data.register.status).toBe(UserStatus.Created)
expect((res.data.register as { password?: string }).password).toBeUndefined()

const meRes = await sdk.me(undefined, { cookie: res.headers.get('set-cookie') })
expect(meRes.data.me.username).toBe(input.username)
expect(meRes.data.me.status).toBe(UserStatus.Created)
expect((meRes.data.me as { password?: string }).password).toBeUndefined()
})
})

describe('unauthorized', () => {
it('should not log in with a short password', async () => {
expect.assertions(1)
try {
await sdk.login({ input: { username: 'alice', password: 'short' } })
} catch (e) {
expect(e.message).toContain('Password is too short.')
}
})

it('should not log in with a wrong password', async () => {
expect.assertions(1)
try {
await sdk.login({ input: { username: 'alice', password: 'wrong password' } })
} catch (e) {
expect(e.message).toContain('Password is incorrect.')
}
})

it('should not log in with user with empty password', async () => {
expect.assertions(1)
try {
await sdk.login({ input: { username: 'charlie', password: 'does-not-have-a-password' } })
} catch (e) {
expect(e.message).toContain('Password login not allowed.')
}
})

it('should not log in with a non-existing user', async () => {
expect.assertions(1)
try {
await sdk.login({ input: { username: uniqueId('user'), password: 'wrong password' } })
} catch (e) {
expect(e.message).toContain('User not found.')
}
})

it('should not log in with an inactive user', async () => {
expect.assertions(1)
try {
await sdk.login({ input: { username: 'dave', password: 'password' } })
} catch (e) {
expect(e.message).toContain('User is inactive.')
}
it('should log in using alice (Admin)', async () => {
const { user } = await getUserCookie(alice)
delete user.createdAt
delete user.updatedAt

expect(user.id).toBe('alice')
expect(user.username).toBe('alice')
expect(user.name).toBe('alice')
expect(user.role).toBe(UserRole.Admin)
expect(user.status).toBe(UserStatus.Active)
expect(user.developer).toBe(true)
expect(user.profileUrl).toBe('/u/alice')
expect(user.avatarUrl).toBeNull()
})
it('should log in using bob (User)', async () => {
const { user } = await getUserCookie(bob)
delete user.createdAt
delete user.updatedAt

expect(user.id).toBe('bob')
expect(user.username).toBe('bob')
expect(user.name).toBe('bob')
expect(user.role).toBe(UserRole.User)
expect(user.status).toBe(UserStatus.Active)
expect(user.developer).toBe(false)
expect(user.profileUrl).toBe('/u/bob')
expect(user.avatarUrl).toBeNull()
})
})
})
Expand Down
51 changes: 22 additions & 29 deletions apps/api-e2e/src/api/api-bot-admin-feature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@ const defaultInput: AdminCreateBotInput = {
xdescribe('api-bot-feature', () => {
describe('api-bot-admin-resolver', () => {
let botId: string
let cookie: string
let alice: string
let bob: string

beforeAll(async () => {
cookie = await getAliceCookie()
const created = await sdk.adminCreateBot({ input: defaultInput }, { cookie })
alice = await getAliceCookie()
bob = await getBobCookie()
const created = await sdk.adminCreateBot({ input: defaultInput }, { cookie: alice })
botId = created.data.created.id
})

describe('authorized', () => {
beforeAll(async () => {
cookie = await getAliceCookie()
})

it('should create a bot', async () => {
const input: AdminCreateBotInput = defaultInput

const res = await sdk.adminCreateBot({ input }, { cookie })
const res = await sdk.adminCreateBot({ input }, { cookie: alice })

const item: Bot = res.data.created

Expand All @@ -42,26 +40,26 @@ xdescribe('api-bot-feature', () => {

it('should update a bot', async () => {
const createInput: AdminCreateBotInput = defaultInput
const createdRes = await sdk.adminCreateBot({ input: createInput }, { cookie })
const createdRes = await sdk.adminCreateBot({ input: createInput }, { cookie: alice })
const botId = createdRes.data.created.id
const input: AdminUpdateBotInput = defaultInput

const res = await sdk.adminUpdateBot({ botId, input }, { cookie })
const res = await sdk.adminUpdateBot({ botId, input }, { cookie: alice })

const item: Bot = res.data.updated
expect(item.name).toBe(input.name)
})

it('should find a list of bots (find all)', async () => {
const createInput: AdminCreateBotInput = defaultInput
const createdRes = await sdk.adminCreateBot({ input: createInput }, { cookie })
const createdRes = await sdk.adminCreateBot({ input: createInput }, { cookie: alice })
const botId = createdRes.data.created.id

const input: AdminFindManyBotInput = {
communityId: defaultCommunityId,
}

const res = await sdk.adminFindManyBot({ input }, { cookie })
const res = await sdk.adminFindManyBot({ input }, { cookie: alice })

expect(res.data.paging.meta.totalCount).toBeGreaterThan(1)
expect(res.data.paging.data.length).toBeGreaterThan(1)
Expand All @@ -71,15 +69,15 @@ xdescribe('api-bot-feature', () => {

it('should find a list of bots (find new one)', async () => {
const createInput: AdminCreateBotInput = defaultInput
const createdRes = await sdk.adminCreateBot({ input: createInput }, { cookie })
const createdRes = await sdk.adminCreateBot({ input: createInput }, { cookie: alice })
const botId = createdRes.data.created.id

const input: AdminFindManyBotInput = {
communityId: defaultCommunityId,
search: botId,
}

const res = await sdk.adminFindManyBot({ input }, { cookie })
const res = await sdk.adminFindManyBot({ input }, { cookie: alice })

expect(res.data.paging.meta.totalCount).toBe(1)
expect(res.data.paging.data.length).toBe(1)
Expand All @@ -88,20 +86,20 @@ xdescribe('api-bot-feature', () => {

it('should find a bot by id', async () => {
const createInput: AdminCreateBotInput = defaultInput
const createdRes = await sdk.adminCreateBot({ input: createInput }, { cookie })
const createdRes = await sdk.adminCreateBot({ input: createInput }, { cookie: alice })
const botId = createdRes.data.created.id

const res = await sdk.adminFindOneBot({ botId }, { cookie })
const res = await sdk.adminFindOneBot({ botId }, { cookie: alice })

expect(res.data.item.id).toBe(botId)
})

it('should delete a bot', async () => {
const createInput: AdminCreateBotInput = defaultInput
const createdRes = await sdk.adminCreateBot({ input: createInput }, { cookie })
const createdRes = await sdk.adminCreateBot({ input: createInput }, { cookie: alice })
const botId = createdRes.data.created.id

const res = await sdk.adminDeleteBot({ botId }, { cookie })
const res = await sdk.adminDeleteBot({ botId }, { cookie: alice })

expect(res.data.deleted).toBe(true)

Expand All @@ -112,25 +110,20 @@ xdescribe('api-bot-feature', () => {
search: botId,
},
},
{ cookie },
{ cookie: alice },
)
expect(findRes.data.paging.meta.totalCount).toBe(0)
expect(findRes.data.paging.data.length).toBe(0)
})
})

describe('unauthorized', () => {
let cookie: string
beforeAll(async () => {
cookie = await getBobCookie()
})

it('should not create a bot', async () => {
expect.assertions(1)
const input: AdminCreateBotInput = defaultInput

try {
await sdk.adminCreateBot({ input }, { cookie })
await sdk.adminCreateBot({ input }, { cookie: bob })
} catch (e) {
expect(e.message).toBe('Unauthorized: User is not Admin')
}
Expand All @@ -139,7 +132,7 @@ xdescribe('api-bot-feature', () => {
it('should not update a bot', async () => {
expect.assertions(1)
try {
await sdk.adminUpdateBot({ botId, input: {} }, { cookie })
await sdk.adminUpdateBot({ botId, input: {} }, { cookie: bob })
} catch (e) {
expect(e.message).toBe('Unauthorized: User is not Admin')
}
Expand All @@ -148,7 +141,7 @@ xdescribe('api-bot-feature', () => {
it('should not find a list of bots (find all)', async () => {
expect.assertions(1)
try {
await sdk.adminFindManyBot({ input: { communityId: defaultCommunityId } }, { cookie })
await sdk.adminFindManyBot({ input: { communityId: defaultCommunityId } }, { cookie: bob })
} catch (e) {
expect(e.message).toBe('Unauthorized: User is not Admin')
}
Expand All @@ -157,7 +150,7 @@ xdescribe('api-bot-feature', () => {
it('should not find a bot by id', async () => {
expect.assertions(1)
try {
await sdk.adminFindOneBot({ botId }, { cookie })
await sdk.adminFindOneBot({ botId }, { cookie: bob })
} catch (e) {
expect(e.message).toBe('Unauthorized: User is not Admin')
}
Expand All @@ -166,7 +159,7 @@ xdescribe('api-bot-feature', () => {
it('should not delete a bot', async () => {
expect.assertions(1)
try {
await sdk.adminDeleteBot({ botId }, { cookie })
await sdk.adminDeleteBot({ botId }, { cookie: bob })
} catch (e) {
expect(e.message).toBe('Unauthorized: User is not Admin')
}
Expand Down
Loading

0 comments on commit 3c9195b

Please sign in to comment.