-
Notifications
You must be signed in to change notification settings - Fork 403
/
handle-invalidation.test.ts
131 lines (114 loc) · 3.78 KB
/
handle-invalidation.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { DAY } from '@atproto/common'
import { TestNetwork, SeedClient } from '@atproto/dev-env'
import { AtpAgent } from '@atproto/api'
import userSeed from './seeds/users'
describe('handle invalidation', () => {
let network: TestNetwork
let agent: AtpAgent
let pdsAgent: AtpAgent
let sc: SeedClient
let alice: string
let bob: string
const mockHandles = {}
beforeAll(async () => {
network = await TestNetwork.create({
dbPostgresSchema: 'bsky_handle_invalidation',
})
agent = network.bsky.getClient()
pdsAgent = network.pds.getClient()
sc = network.getSeedClient()
await userSeed(sc)
await network.processAll()
alice = sc.dids.alice
bob = sc.dids.bob
const origResolve = network.bsky.indexer.ctx.idResolver.handle.resolve
network.bsky.indexer.ctx.idResolver.handle.resolve = async (
handle: string,
) => {
if (mockHandles[handle] === null) {
return undefined
} else if (mockHandles[handle]) {
return mockHandles[handle]
}
return origResolve(handle)
}
})
afterAll(async () => {
await network.close()
})
const backdateIndexedAt = async (did: string) => {
const TWO_DAYS_AGO = new Date(Date.now() - 2 * DAY).toISOString()
await network.bsky.ctx.db
.getPrimary()
.db.updateTable('actor')
.set({ indexedAt: TWO_DAYS_AGO })
.where('did', '=', did)
.execute()
}
it('indexes an account with no proper handle', async () => {
mockHandles['eve.test'] = null
const eveAccnt = await sc.createAccount('eve', {
handle: 'eve.test',
email: 'eve@test.com',
password: 'eve-pass',
})
await network.processAll()
const res = await agent.api.app.bsky.actor.getProfile(
{ actor: eveAccnt.did },
{ headers: await network.serviceHeaders(alice) },
)
expect(res.data.handle).toEqual('handle.invalid')
})
it('invalidates out of date handles', async () => {
await backdateIndexedAt(alice)
const aliceHandle = sc.accounts[alice].handle
// alice's handle no longer resolves
mockHandles[aliceHandle] = null
await sc.post(alice, 'blah')
await network.processAll()
const res = await agent.api.app.bsky.actor.getProfile(
{ actor: alice },
{ headers: await network.serviceHeaders(alice) },
)
expect(res.data.handle).toEqual('handle.invalid')
})
it('revalidates an out of date handle', async () => {
await backdateIndexedAt(alice)
const aliceHandle = sc.accounts[alice].handle
// alice's handle no longer resolves
delete mockHandles[aliceHandle]
await sc.post(alice, 'blah')
await network.processAll()
const res = await agent.api.app.bsky.actor.getProfile(
{ actor: alice },
{ headers: await network.serviceHeaders(alice) },
)
expect(res.data.handle).toEqual(sc.accounts[alice].handle)
})
it('deals with handle contention', async () => {
await backdateIndexedAt(bob)
// update alices handle so that the pds will let bob take her old handle
await network.pds.ctx.db.db
.updateTable('account')
.where('did', '=', alice)
.set({ handle: 'not-alice.test' })
.execute()
await pdsAgent.api.com.atproto.identity.updateHandle(
{
handle: sc.accounts[alice].handle,
},
{ headers: sc.getHeaders(bob), encoding: 'application/json' },
)
await network.processAll()
const aliceRes = await agent.api.app.bsky.actor.getProfile(
{ actor: alice },
{ headers: await network.serviceHeaders(alice) },
)
expect(aliceRes.data.handle).toEqual('handle.invalid')
const bobRes = await agent.api.app.bsky.actor.getProfile(
{ actor: bob },
{ headers: await network.serviceHeaders(alice) },
)
expect(bobRes.data.handle).toEqual(sc.accounts[alice].handle)
})
})