Skip to content

Commit

Permalink
feat: start interaction with on-chain program
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Mar 17, 2024
1 parent 47bb3a2 commit 6e1c899
Show file tree
Hide file tree
Showing 60 changed files with 1,385 additions and 88 deletions.
13 changes: 13 additions & 0 deletions api-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ input ProfileUserUpdateInput {
username: String
}

enum PubKeyIdentityProvider {
Discord
Solana
}

type Query {
adminFindManyIdentity(input: IdentityAdminFindManyInput!): [Identity!]
adminFindManyProfile(input: ProfileAdminFindManyInput!): ProfilePaging!
Expand All @@ -188,6 +193,14 @@ type Query {
anonRequestIdentityChallenge(input: IdentityRequestChallengeInput!): IdentityChallenge
appConfig: AppConfig!
me: User
profileGetPointer(account: String!): JSON
profileGetPointerPda(provider: PubKeyIdentityProvider!, providerId: String!): JSON
profileGetPointers: JSON
profileGetProfile(account: String!): JSON
profileGetProfileByUsername(username: String!): JSON
profileGetProfilePda(username: String!): JSON
profileGetProfiles: JSON
profileGetProgramAccount: JSON
uptime: Float!
userFindManyIdentity(input: IdentityUserFindManyInput!): [Identity!]
userFindManyProfile(input: ProfileUserFindManyInput!): ProfilePaging!
Expand Down
75 changes: 48 additions & 27 deletions apps/api-e2e/src/api/api-profile-admin-feature.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
AdminCreateProfileInput,
AdminFindManyProfileInput,
AdminUpdateProfileInput,
Profile,
ProfileAdminCreateInput,
ProfileAdminFindManyInput,
ProfileAdminUpdateInput,
} from '@pubkey-program-sandbox/sdk'
import { getAliceCookie, getBobCookie, sdk, uniqueId } from '../support'

Expand All @@ -14,7 +14,10 @@ describe('api-profile-feature', () => {

beforeAll(async () => {
cookie = await getAliceCookie()
const created = await sdk.adminCreateProfile({ input: { name: profileName } }, { cookie })
const created = await sdk.adminCreateProfile(
{ input: { username: profileName, account: profileName, ownerId: 'alice' } },
{ cookie },
)
profileId = created.data.created.id
})

Expand All @@ -24,60 +27,72 @@ describe('api-profile-feature', () => {
})

it('should create a profile', async () => {
const input: AdminCreateProfileInput = {
name: uniqueId('profile'),
const input: ProfileAdminCreateInput = {
ownerId: 'alice',
account: uniqueId('profile'),
username: uniqueId('profile'),
}

const res = await sdk.adminCreateProfile({ input }, { cookie })

const item: Profile = res.data.created
expect(item.name).toBe(input.name)
expect(item.username).toBe(input.username)
expect(item.id).toBeDefined()
expect(item.createdAt).toBeDefined()
expect(item.updatedAt).toBeDefined()
})

it('should update a profile', async () => {
const createInput: AdminCreateProfileInput = {
name: uniqueId('profile'),
const createInput: ProfileAdminCreateInput = {
ownerId: 'alice',
account: uniqueId('profile'),
username: uniqueId('profile'),
}
const createdRes = await sdk.adminCreateProfile({ input: createInput }, { cookie })
const profileId = createdRes.data.created.id
const input: AdminUpdateProfileInput = {
name: uniqueId('profile'),
const input: ProfileAdminUpdateInput = {
account: uniqueId('profile'),
username: uniqueId('profile'),
}

const res = await sdk.adminUpdateProfile({ profileId, input }, { cookie })

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

it('should find a list of profiles (find all)', async () => {
const createInput: AdminCreateProfileInput = {
name: uniqueId('profile'),
const createInput: ProfileAdminCreateInput = {
ownerId: 'alice',
account: uniqueId('profile'),
username: uniqueId('profile'),
}
const createdRes = await sdk.adminCreateProfile({ input: createInput }, { cookie })
const profileId = createdRes.data.created.id

const input: AdminFindManyProfileInput = {}
const input: ProfileAdminFindManyInput = {
ownerId: 'alice',
}

const res = await sdk.adminFindManyProfile({ input }, { cookie })

expect(res.data.paging.meta.totalCount).toBeGreaterThan(1)
expect(res.data.paging.data.length).toBeGreaterThan(1)
// First item should be the one we created above
expect(res.data.paging.data[0].id).toBe(profileId)
expect(res.data.paging.data.map((i) => i.id)).toContain(profileId)
})

it('should find a list of profiles (find new one)', async () => {
const createInput: AdminCreateProfileInput = {
name: uniqueId('profile'),
const createInput: ProfileAdminCreateInput = {
ownerId: 'alice',
account: uniqueId('profile'),
username: uniqueId('profile'),
}
const createdRes = await sdk.adminCreateProfile({ input: createInput }, { cookie })
const profileId = createdRes.data.created.id

const input: AdminFindManyProfileInput = {
const input: ProfileAdminFindManyInput = {
ownerId: 'alice',
search: profileId,
}

Expand All @@ -89,8 +104,10 @@ describe('api-profile-feature', () => {
})

it('should find a profile by id', async () => {
const createInput: AdminCreateProfileInput = {
name: uniqueId('profile'),
const createInput: ProfileAdminCreateInput = {
ownerId: 'alice',
account: uniqueId('profile'),
username: uniqueId('profile'),
}
const createdRes = await sdk.adminCreateProfile({ input: createInput }, { cookie })
const profileId = createdRes.data.created.id
Expand All @@ -101,8 +118,10 @@ describe('api-profile-feature', () => {
})

it('should delete a profile', async () => {
const createInput: AdminCreateProfileInput = {
name: uniqueId('profile'),
const createInput: ProfileAdminCreateInput = {
ownerId: 'alice',
account: uniqueId('profile'),
username: uniqueId('profile'),
}
const createdRes = await sdk.adminCreateProfile({ input: createInput }, { cookie })
const profileId = createdRes.data.created.id
Expand All @@ -111,7 +130,7 @@ describe('api-profile-feature', () => {

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

const findRes = await sdk.adminFindManyProfile({ input: { search: profileId } }, { cookie })
const findRes = await sdk.adminFindManyProfile({ input: { ownerId: 'alice', search: profileId } }, { cookie })
expect(findRes.data.paging.meta.totalCount).toBe(0)
expect(findRes.data.paging.data.length).toBe(0)
})
Expand All @@ -125,8 +144,10 @@ describe('api-profile-feature', () => {

it('should not create a profile', async () => {
expect.assertions(1)
const input: AdminCreateProfileInput = {
name: uniqueId('profile'),
const input: ProfileAdminCreateInput = {
ownerId: 'alice',
account: uniqueId('profile'),
username: uniqueId('profile'),
}

try {
Expand All @@ -148,7 +169,7 @@ describe('api-profile-feature', () => {
it('should not find a list of profiles (find all)', async () => {
expect.assertions(1)
try {
await sdk.adminFindManyProfile({ input: {} }, { cookie })
await sdk.adminFindManyProfile({ input: { ownerId: 'alice' } }, { cookie })
} catch (e) {
expect(e.message).toBe('Unauthorized: User is not Admin')
}
Expand Down
84 changes: 36 additions & 48 deletions apps/api-e2e/src/api/api-profile-user-feature.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
UserCreateProfileInput,
UserFindManyProfileInput,
UserUpdateProfileInput,
Profile,
ProfileUserCreateInput,
ProfileUserFindManyInput,
ProfileUserUpdateInput,
} from '@pubkey-program-sandbox/sdk'
import { getAliceCookie, getBobCookie, sdk, uniqueId } from '../support'

Expand All @@ -14,7 +14,10 @@ describe('api-profile-feature', () => {

beforeAll(async () => {
cookie = await getAliceCookie()
const created = await sdk.userCreateProfile({ input: { name: profileName } }, { cookie })
const created = await sdk.userCreateProfile(
{ input: { username: profileName, account: profileName } },
{ cookie },
)
profileId = created.data.created.id
})

Expand All @@ -24,60 +27,65 @@ describe('api-profile-feature', () => {
})

it('should create a profile', async () => {
const input: UserCreateProfileInput = {
name: uniqueId('profile'),
const input: ProfileUserCreateInput = {
account: uniqueId('profile'),
username: uniqueId('profile'),
}

const res = await sdk.userCreateProfile({ input }, { cookie })

const item: Profile = res.data.created
expect(item.name).toBe(input.name)
expect(item.username).toBe(input.username)
expect(item.id).toBeDefined()
expect(item.createdAt).toBeDefined()
expect(item.updatedAt).toBeDefined()
})

it('should update a profile', async () => {
const createInput: UserCreateProfileInput = {
name: uniqueId('profile'),
const createInput: ProfileUserCreateInput = {
account: uniqueId('profile'),
username: uniqueId('profile'),
}
const createdRes = await sdk.userCreateProfile({ input: createInput }, { cookie })
const profileId = createdRes.data.created.id
const input: UserUpdateProfileInput = {
name: uniqueId('profile'),
const input: ProfileUserUpdateInput = {
account: uniqueId('profile'),
username: uniqueId('profile'),
}

const res = await sdk.userUpdateProfile({ profileId, input }, { cookie })

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

it('should find a list of profiles (find all)', async () => {
const createInput: UserCreateProfileInput = {
name: uniqueId('profile'),
const createInput: ProfileUserCreateInput = {
account: uniqueId('profile'),
username: uniqueId('profile'),
}
const createdRes = await sdk.userCreateProfile({ input: createInput }, { cookie })
const profileId = createdRes.data.created.id

const input: UserFindManyProfileInput = {}
const input: ProfileUserFindManyInput = {}

const res = await sdk.userFindManyProfile({ input }, { cookie })

expect(res.data.paging.meta.totalCount).toBeGreaterThan(1)
expect(res.data.paging.data.length).toBeGreaterThan(1)
// First item should be the one we created above
expect(res.data.paging.data[0].id).toBe(profileId)
expect(res.data.paging.data.map((i) => i.id)).toContain(profileId)
})

it('should find a list of profiles (find new one)', async () => {
const createInput: UserCreateProfileInput = {
name: uniqueId('profile'),
const createInput: ProfileUserCreateInput = {
account: uniqueId('profile'),
username: uniqueId('profile'),
}
const createdRes = await sdk.userCreateProfile({ input: createInput }, { cookie })
const profileId = createdRes.data.created.id

const input: UserFindManyProfileInput = {
const input: ProfileUserFindManyInput = {
search: profileId,
}

Expand All @@ -89,8 +97,9 @@ describe('api-profile-feature', () => {
})

it('should find a profile by id', async () => {
const createInput: UserCreateProfileInput = {
name: uniqueId('profile'),
const createInput: ProfileUserCreateInput = {
account: uniqueId('profile'),
username: uniqueId('profile'),
}
const createdRes = await sdk.userCreateProfile({ input: createInput }, { cookie })
const profileId = createdRes.data.created.id
Expand All @@ -101,8 +110,9 @@ describe('api-profile-feature', () => {
})

it('should delete a profile', async () => {
const createInput: UserCreateProfileInput = {
name: uniqueId('profile'),
const createInput: ProfileUserCreateInput = {
account: uniqueId('profile'),
username: uniqueId('profile'),
}
const createdRes = await sdk.userCreateProfile({ input: createInput }, { cookie })
const profileId = createdRes.data.created.id
Expand All @@ -123,34 +133,12 @@ describe('api-profile-feature', () => {
cookie = await getBobCookie()
})

it('should not create a profile', async () => {
expect.assertions(1)
const input: UserCreateProfileInput = {
name: uniqueId('profile'),
}

try {
await sdk.userCreateProfile({ input }, { cookie })
} catch (e) {
expect(e.message).toBe('Unauthorized: User is not User')
}
})

it('should not update a profile', async () => {
expect.assertions(1)
try {
await sdk.userUpdateProfile({ profileId, input: {} }, { cookie })
} catch (e) {
expect(e.message).toBe('Unauthorized: User is not User')
}
})

it('should not find a list of profiles (find all)', async () => {
expect.assertions(1)
try {
await sdk.userFindManyProfile({ input: {} }, { cookie })
} catch (e) {
expect(e.message).toBe('Unauthorized: User is not User')
expect(e.message).toBe('You are not authorized to update this Profile')
}
})

Expand All @@ -159,7 +147,7 @@ describe('api-profile-feature', () => {
try {
await sdk.userFindOneProfile({ profileId }, { cookie })
} catch (e) {
expect(e.message).toBe('Unauthorized: User is not User')
expect(e.message).toBe('You are not authorized to view this Profile')
}
})

Expand All @@ -168,7 +156,7 @@ describe('api-profile-feature', () => {
try {
await sdk.userDeleteProfile({ profileId }, { cookie })
} catch (e) {
expect(e.message).toBe('Unauthorized: User is not User')
expect(e.message).toBe('You are not authorized to delete this Profile')
}
})
})
Expand Down
Loading

0 comments on commit 6e1c899

Please sign in to comment.