-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import assert from 'assert'; | ||
import { | ||
Image, | ||
} from '@fedify/fedify'; | ||
import sinon from 'sinon'; | ||
|
||
import { | ||
ACTOR_DEFAULT_ICON, | ||
ACTOR_DEFAULT_NAME, | ||
ACTOR_DEFAULT_SUMMARY | ||
} from './constants'; | ||
import { getUserData } from './user'; | ||
|
||
|
||
const HANDLE = 'foo'; | ||
|
||
function getCtx() { | ||
const host = 'example.com'; | ||
|
||
const ctx = { | ||
data: { | ||
db: { | ||
get: sinon.stub(), | ||
set: sinon.stub(), | ||
}, | ||
}, | ||
getActorKeyPairs: sinon.stub(), | ||
getActorUri: sinon.stub(), | ||
getInboxUri: sinon.stub(), | ||
getOutboxUri: sinon.stub(), | ||
getFollowingUri: sinon.stub(), | ||
getFollowersUri: sinon.stub(), | ||
host, | ||
}; | ||
|
||
ctx.getActorKeyPairs.withArgs(HANDLE).resolves([ | ||
{ cryptographicKey: 'abc123' } | ||
]); | ||
|
||
ctx.getActorUri.withArgs(HANDLE).returns(new URL(`https://${host}/${HANDLE}`)); | ||
ctx.getInboxUri.withArgs(HANDLE).returns(new URL(`https://${host}/${HANDLE}/inbox`)); | ||
ctx.getOutboxUri.withArgs(HANDLE).returns(new URL(`https://${host}/${HANDLE}/outbox`)); | ||
ctx.getFollowingUri.withArgs(HANDLE).returns(new URL(`https://${host}/${HANDLE}/following`)); | ||
ctx.getFollowersUri.withArgs(HANDLE).returns(new URL(`https://${host}/${HANDLE}/followers`)); | ||
|
||
return ctx; | ||
} | ||
|
||
describe('getUserData', function () { | ||
it('persists a user to the database if it does not exist', async function () { | ||
const ctx = getCtx(); | ||
|
||
ctx.data.db.get.resolves(null); | ||
|
||
const result = await getUserData(ctx, HANDLE); | ||
|
||
const expectedUserData = { | ||
id: new URL(`https://${ctx.host}/${HANDLE}`), | ||
name: ACTOR_DEFAULT_NAME, | ||
summary: ACTOR_DEFAULT_SUMMARY, | ||
preferredUsername: HANDLE, | ||
icon: new Image({ url: new URL(ACTOR_DEFAULT_ICON) }), | ||
inbox: new URL(`https://${ctx.host}/${HANDLE}/inbox`), | ||
outbox: new URL(`https://${ctx.host}/${HANDLE}/outbox`), | ||
following: new URL(`https://${ctx.host}/${HANDLE}/following`), | ||
followers: new URL(`https://${ctx.host}/${HANDLE}/followers`), | ||
publicKeys: ['abc123'], | ||
url: new URL(`https://${ctx.host}`), | ||
} | ||
|
||
assert.ok( | ||
ctx.data.db.set.calledWith(['handle', HANDLE], { | ||
id: expectedUserData.id.href, | ||
name: expectedUserData.name, | ||
summary: expectedUserData.summary, | ||
preferredUsername: expectedUserData.preferredUsername, | ||
icon: ACTOR_DEFAULT_ICON, | ||
inbox: expectedUserData.inbox.href, | ||
outbox: expectedUserData.outbox.href, | ||
following: expectedUserData.following.href, | ||
followers: expectedUserData.followers.href, | ||
url: expectedUserData.url.href, | ||
}) | ||
); | ||
assert.deepStrictEqual(result, expectedUserData); | ||
}); | ||
}); |