Skip to content

Commit 8fcd9cf

Browse files
committed
feat: scaffold Log feature
1 parent 9f03198 commit 8fcd9cf

File tree

82 files changed

+2403
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2403
-12
lines changed

api-schema.graphql

+72
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ input AdminFindManyIdentityInput {
8181
provider: IdentityProvider
8282
}
8383

84+
input AdminFindManyLogInput {
85+
botId: String
86+
communityId: String!
87+
identityProvider: IdentityProvider
88+
identityProviderId: String
89+
level: LogLevel
90+
limit: Int = 10
91+
page: Int = 1
92+
relatedId: String
93+
relatedType: LogRelatedType
94+
ruleId: String
95+
search: String
96+
userId: String
97+
}
98+
8499
input AdminFindManyNetworkInput {
85100
limit: Int = 10
86101
page: Int = 1
@@ -322,6 +337,42 @@ input LinkIdentityInput {
322337
providerId: String!
323338
}
324339

340+
type Log {
341+
botId: String
342+
communityId: String!
343+
createdAt: DateTime
344+
data: JSON
345+
id: String!
346+
identityProvider: IdentityProvider
347+
identityProviderId: String
348+
level: LogLevel!
349+
message: String!
350+
relatedId: String
351+
relatedType: LogRelatedType
352+
ruleId: String
353+
updatedAt: DateTime
354+
userId: String
355+
}
356+
357+
enum LogLevel {
358+
Error
359+
Info
360+
Warning
361+
}
362+
363+
type LogPaging {
364+
data: [Log!]!
365+
meta: PagingMeta!
366+
}
367+
368+
enum LogRelatedType {
369+
Bot
370+
Community
371+
Identity
372+
Rule
373+
User
374+
}
375+
325376
input LoginInput {
326377
password: String!
327378
username: String!
@@ -342,6 +393,7 @@ type Mutation {
342393
adminDeleteCommunity(communityId: String!): Boolean
343394
adminDeleteCommunityMember(communityMemberId: String!): Boolean
344395
adminDeleteIdentity(identityId: String!): Boolean
396+
adminDeleteLog(logId: String!): Boolean
345397
adminDeleteNetwork(networkId: String!): Boolean
346398
adminDeleteNetworkToken(networkTokenId: String!): Boolean
347399
adminDeleteRule(ruleId: String!): Boolean
@@ -370,6 +422,7 @@ type Mutation {
370422
userDeleteCommunity(communityId: String!): Boolean
371423
userDeleteCommunityMember(communityMemberId: String!): Boolean
372424
userDeleteIdentity(identityId: String!): Boolean
425+
userDeleteLog(logId: String!): Boolean
373426
userDeleteRule(ruleId: String!): Boolean
374427
userDeleteRuleCondition(ruleConditionId: String!): Boolean
375428
userDeleteRulePermission(rulePermissionId: String!): Boolean
@@ -467,13 +520,15 @@ type Query {
467520
adminFindManyCommunity(input: AdminFindManyCommunityInput!): CommunityPaging!
468521
adminFindManyCommunityMember(input: AdminFindManyCommunityMemberInput!): CommunityMemberPaging!
469522
adminFindManyIdentity(input: AdminFindManyIdentityInput!): [Identity!]
523+
adminFindManyLog(input: AdminFindManyLogInput!): LogPaging!
470524
adminFindManyNetwork(input: AdminFindManyNetworkInput!): NetworkPaging!
471525
adminFindManyNetworkToken(input: AdminFindManyNetworkTokenInput!): NetworkTokenPaging!
472526
adminFindManyRule(input: AdminFindManyRuleInput!): RulePaging!
473527
adminFindManyUser(input: AdminFindManyUserInput!): UserPaging!
474528
adminFindOneBot(botId: String!): Bot
475529
adminFindOneCommunity(communityId: String!): Community
476530
adminFindOneCommunityMember(communityMemberId: String!): CommunityMember
531+
adminFindOneLog(logId: String!): Log
477532
adminFindOneNetwork(networkId: String!): Network
478533
adminFindOneNetworkToken(networkTokenId: String!): NetworkToken
479534
adminFindOneRule(ruleId: String!): Rule
@@ -488,12 +543,14 @@ type Query {
488543
userFindManyCommunity(input: UserFindManyCommunityInput!): CommunityPaging!
489544
userFindManyCommunityMember(input: UserFindManyCommunityMemberInput!): CommunityMemberPaging!
490545
userFindManyIdentity(input: UserFindManyIdentityInput!): [Identity!]
546+
userFindManyLog(input: UserFindManyLogInput!): LogPaging!
491547
userFindManyNetworkToken(input: UserFindManyNetworkTokenInput!): NetworkTokenPaging!
492548
userFindManyRule(input: UserFindManyRuleInput!): RulePaging!
493549
userFindManyUser(input: UserFindManyUserInput!): UserPaging!
494550
userFindOneBot(communityId: String!): Bot
495551
userFindOneCommunity(communityId: String!): Community
496552
userFindOneCommunityMember(communityMemberId: String!): CommunityMember
553+
userFindOneLog(logId: String!): Log
497554
userFindOneRule(ruleId: String!): Rule
498555
userFindOneUser(username: String!): User
499556
userGetBotMembers(botId: String!, serverId: String!): [BotMember!]
@@ -643,6 +700,21 @@ input UserFindManyIdentityInput {
643700
username: String!
644701
}
645702

703+
input UserFindManyLogInput {
704+
botId: String
705+
communityId: String!
706+
identityProvider: IdentityProvider
707+
identityProviderId: String
708+
level: LogLevel
709+
limit: Int = 10
710+
page: Int = 1
711+
relatedId: String
712+
relatedType: LogRelatedType
713+
ruleId: String
714+
search: String
715+
userId: String
716+
}
717+
646718
input UserFindManyNetworkTokenInput {
647719
cluster: NetworkCluster!
648720
limit: Int = 10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import { AdminCreateLogInput, AdminFindManyLogInput, AdminUpdateLogInput, Log } from '@pubkey-link/sdk'
2+
import { getAliceCookie, getBobCookie, sdk, uniqueId } from '../support'
3+
4+
describe('api-log-feature', () => {
5+
describe('api-log-admin-resolver', () => {
6+
const logName = uniqueId('acme-log')
7+
let logId: string
8+
let cookie: string
9+
10+
beforeAll(async () => {
11+
cookie = await getAliceCookie()
12+
const created = await sdk.adminCreateLog({ input: { name: logName } }, { cookie })
13+
logId = created.data.created.id
14+
})
15+
16+
describe('authorized', () => {
17+
beforeAll(async () => {
18+
cookie = await getAliceCookie()
19+
})
20+
21+
it('should create a log', async () => {
22+
const input: AdminCreateLogInput = {
23+
name: uniqueId('log'),
24+
}
25+
26+
const res = await sdk.adminCreateLog({ input }, { cookie })
27+
28+
const item: Log = res.data.created
29+
expect(item.name).toBe(input.name)
30+
expect(item.id).toBeDefined()
31+
expect(item.createdAt).toBeDefined()
32+
expect(item.updatedAt).toBeDefined()
33+
})
34+
35+
it('should update a log', async () => {
36+
const createInput: AdminCreateLogInput = {
37+
name: uniqueId('log'),
38+
}
39+
const createdRes = await sdk.adminCreateLog({ input: createInput }, { cookie })
40+
const logId = createdRes.data.created.id
41+
const input: AdminUpdateLogInput = {
42+
name: uniqueId('log'),
43+
}
44+
45+
const res = await sdk.adminUpdateLog({ logId, input }, { cookie })
46+
47+
const item: Log = res.data.updated
48+
expect(item.name).toBe(input.name)
49+
})
50+
51+
it('should find a list of logs (find all)', async () => {
52+
const createInput: AdminCreateLogInput = {
53+
name: uniqueId('log'),
54+
}
55+
const createdRes = await sdk.adminCreateLog({ input: createInput }, { cookie })
56+
const logId = createdRes.data.created.id
57+
58+
const input: AdminFindManyLogInput = {}
59+
60+
const res = await sdk.adminFindManyLog({ input }, { cookie })
61+
62+
expect(res.data.paging.meta.totalCount).toBeGreaterThan(1)
63+
expect(res.data.paging.data.length).toBeGreaterThan(1)
64+
// First item should be the one we created above
65+
expect(res.data.paging.data[0].id).toBe(logId)
66+
})
67+
68+
it('should find a list of logs (find new one)', async () => {
69+
const createInput: AdminCreateLogInput = {
70+
name: uniqueId('log'),
71+
}
72+
const createdRes = await sdk.adminCreateLog({ input: createInput }, { cookie })
73+
const logId = createdRes.data.created.id
74+
75+
const input: AdminFindManyLogInput = {
76+
search: logId,
77+
}
78+
79+
const res = await sdk.adminFindManyLog({ input }, { cookie })
80+
81+
expect(res.data.paging.meta.totalCount).toBe(1)
82+
expect(res.data.paging.data.length).toBe(1)
83+
expect(res.data.paging.data[0].id).toBe(logId)
84+
})
85+
86+
it('should find a log by id', async () => {
87+
const createInput: AdminCreateLogInput = {
88+
name: uniqueId('log'),
89+
}
90+
const createdRes = await sdk.adminCreateLog({ input: createInput }, { cookie })
91+
const logId = createdRes.data.created.id
92+
93+
const res = await sdk.adminFindOneLog({ logId }, { cookie })
94+
95+
expect(res.data.item.id).toBe(logId)
96+
})
97+
98+
it('should delete a log', async () => {
99+
const createInput: AdminCreateLogInput = {
100+
name: uniqueId('log'),
101+
}
102+
const createdRes = await sdk.adminCreateLog({ input: createInput }, { cookie })
103+
const logId = createdRes.data.created.id
104+
105+
const res = await sdk.adminDeleteLog({ logId }, { cookie })
106+
107+
expect(res.data.deleted).toBe(true)
108+
109+
const findRes = await sdk.adminFindManyLog({ input: { search: logId } }, { cookie })
110+
expect(findRes.data.paging.meta.totalCount).toBe(0)
111+
expect(findRes.data.paging.data.length).toBe(0)
112+
})
113+
})
114+
115+
describe('unauthorized', () => {
116+
let cookie: string
117+
beforeAll(async () => {
118+
cookie = await getBobCookie()
119+
})
120+
121+
it('should not create a log', async () => {
122+
expect.assertions(1)
123+
const input: AdminCreateLogInput = {
124+
name: uniqueId('log'),
125+
}
126+
127+
try {
128+
await sdk.adminCreateLog({ input }, { cookie })
129+
} catch (e) {
130+
expect(e.message).toBe('Unauthorized: User is not Admin')
131+
}
132+
})
133+
134+
it('should not update a log', async () => {
135+
expect.assertions(1)
136+
try {
137+
await sdk.adminUpdateLog({ logId, input: {} }, { cookie })
138+
} catch (e) {
139+
expect(e.message).toBe('Unauthorized: User is not Admin')
140+
}
141+
})
142+
143+
it('should not find a list of logs (find all)', async () => {
144+
expect.assertions(1)
145+
try {
146+
await sdk.adminFindManyLog({ input: {} }, { cookie })
147+
} catch (e) {
148+
expect(e.message).toBe('Unauthorized: User is not Admin')
149+
}
150+
})
151+
152+
it('should not find a log by id', async () => {
153+
expect.assertions(1)
154+
try {
155+
await sdk.adminFindOneLog({ logId }, { cookie })
156+
} catch (e) {
157+
expect(e.message).toBe('Unauthorized: User is not Admin')
158+
}
159+
})
160+
161+
it('should not delete a log', async () => {
162+
expect.assertions(1)
163+
try {
164+
await sdk.adminDeleteLog({ logId }, { cookie })
165+
} catch (e) {
166+
expect(e.message).toBe('Unauthorized: User is not Admin')
167+
}
168+
})
169+
})
170+
})
171+
})

0 commit comments

Comments
 (0)