-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for access manager. (#164)
- Loading branch information
1 parent
e55b101
commit 672393d
Showing
4 changed files
with
299 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { | ||
Chat | ||
} from "../dist-test" | ||
import { | ||
createChatInstance, | ||
makeid, | ||
sleep | ||
} from "./utils"; | ||
|
||
import { jest } from "@jest/globals" | ||
|
||
describe("Access Manager test test", () => { | ||
jest.retryTimes(2) | ||
|
||
let chatPamClient: Chat | ||
let chatPamServer: Chat | ||
|
||
beforeAll(async () => { | ||
let chatClientUserId = makeid(8) | ||
chatPamServer = await createChatInstance( { shouldCreateNewInstance: true, clientType: 'PamServer' }) | ||
const token = await _grantTokenForUserId(chatPamServer, chatClientUserId); | ||
chatPamClient = await createChatInstance( { | ||
userId: chatClientUserId , | ||
shouldCreateNewInstance: true, | ||
clientType: 'PamClient', | ||
config: { | ||
authKey: token | ||
}, | ||
}) | ||
}) | ||
|
||
afterEach(async () => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
// test is skipped because it has 65sec sleep to wait for token expiration | ||
test.skip("when token is updated then client can use API", async () => { | ||
const user1Id = `user1_${Date.now()}` | ||
const userToChatWith = await chatPamServer.createUser(user1Id, { name: "User1" }) | ||
const createDirectConversationResult = await chatPamServer.createDirectConversation( | ||
{ | ||
user: userToChatWith, | ||
channelData: { | ||
name: "Quick sync on customer XYZ" | ||
}, | ||
membershipData: { | ||
custom: { | ||
purpose: "premium-support" | ||
} | ||
} | ||
} | ||
) | ||
let channelId = createDirectConversationResult.channel.id | ||
let token = await _grantTokenForChannel(1, chatPamServer, channelId); | ||
await chatPamClient.sdk.setToken(token) | ||
|
||
const channelRetrievedByClient = await chatPamClient.getChannel(createDirectConversationResult.channel.id); | ||
expect(channelRetrievedByClient).toBeDefined(); | ||
|
||
// Verify that the fetched channel ID matches the expected channel ID | ||
expect(channelRetrievedByClient?.id).toEqual(channelId); | ||
|
||
let publishResult = await channelRetrievedByClient.sendText("my first message"); | ||
let message = await channelRetrievedByClient.getMessage(publishResult.timetoken); | ||
await message.toggleReaction("one") | ||
|
||
// sleep so that token expires | ||
await sleep(65000) | ||
token = await _grantTokenForChannel(1, chatPamServer, channelId); | ||
await chatPamClient.sdk.setToken(token) | ||
|
||
await message.toggleReaction("two"); | ||
await chatPamClient.getChannel(channelRetrievedByClient?.id); | ||
|
||
await chatPamServer.deleteChannel(channelId) | ||
}, 100000); // this long timeout is needed so we can wait 65sec for token to expire | ||
|
||
async function _grantTokenForUserId(chatPamServer, chatClientUserId) { | ||
return chatPamServer.sdk.grantToken({ | ||
ttl: 10, | ||
resources: { | ||
uuids: { | ||
[chatClientUserId]: { | ||
get: true, | ||
update: true | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
async function _grantTokenForChannel(ttl, chatPamServer, channelId) { | ||
return chatPamServer.sdk.grantToken({ | ||
ttl: ttl, | ||
resources: { | ||
channels: { | ||
[channelId]: { | ||
read: true, | ||
write: true, | ||
get: true, // this is important | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
}) |
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
Oops, something went wrong.