-
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.
AccessManager is based on PAM V3 token instead of PAM V2 authKey. (#104)
We encourage new customers to use PAM V3 instead of V2. PAM V3 has pubnub.setToken(token) method to update token whereas in V2 to update authKey it was required to instantiate PubNub with new authKey. From now on customer must use PAM V3 in Kotlin SDK to be able to fully use setLastReadMessageTimetoken method in Chat SDK so that Receipt event is sent only when client has valid token with proper permissions.
- Loading branch information
1 parent
c6e9487
commit cd0571f
Showing
10 changed files
with
143 additions
and
42 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
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
96 changes: 96 additions & 0 deletions
96
pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/integration/AccessManagerTest.kt
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,96 @@ | ||
package com.pubnub.integration | ||
|
||
import com.pubnub.api.PubNubException | ||
import com.pubnub.api.models.consumer.access_manager.v3.ChannelGrant | ||
import com.pubnub.api.models.consumer.access_manager.v3.UUIDGrant | ||
import com.pubnub.chat.Event | ||
import com.pubnub.chat.internal.message.MessageImpl | ||
import com.pubnub.chat.listenForEvents | ||
import com.pubnub.chat.types.EventContent | ||
import com.pubnub.test.await | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
|
||
class AccessManagerTest : BaseChatIntegrationTest() { | ||
@Test | ||
fun pubNubClient_with_PAM_enabled_should_not_getChannel_when_no_token_set() = runTest { | ||
val channelId = channelPam.id | ||
val expectedStatusCode = 403 // Forbidden, no valid security Token provided | ||
|
||
val exception = assertFailsWith<PubNubException> { chatPamClient.getChannel(channelId).await() } | ||
assertEquals(expectedStatusCode, exception.statusCode) | ||
} | ||
|
||
@Test | ||
fun pubNubClient_with_PAM_enabled_should_getChannel_when_token_set() = runTest { | ||
// getToken from server | ||
val channelId = channelPam.id | ||
chatPamServer.createChannel(id = channelId).await() | ||
val token = chatPamServer.pubNub.grantToken( | ||
ttl = 1, | ||
channels = listOf(ChannelGrant.name(get = true, name = channelId, read = true, write = true, manage = true)) // get = true | ||
).await().token | ||
// client uses token generated by server | ||
chatPamClient.pubNub.setToken(token) | ||
|
||
val token1 = chatPamClient.pubNub.getToken() | ||
assertEquals(token1, token) | ||
val actualChannelId = chatPamClient.getChannel(channelId).await()?.id | ||
assertEquals(channelId, actualChannelId) | ||
|
||
chatPamServer.deleteChannel(id = channelId).await() | ||
} | ||
|
||
@Test | ||
fun setLastReadMessageTimetoken_should_send_Receipt_event_when_has_token() = runTest { | ||
var numberOfReceiptEvents = 0 | ||
val timetoken = 1000L | ||
val channelId = channelPam.id | ||
|
||
// server generates token | ||
val token = chatPamServer.pubNub.grantToken( | ||
ttl = 1, | ||
authorizedUUID = chatPamClient.currentUser.id, | ||
channels = listOf( | ||
ChannelGrant.name( | ||
name = channelId, | ||
read = true, | ||
write = true, | ||
get = true, | ||
join = true, | ||
update = true, | ||
manage = true, | ||
delete = true, | ||
create = true | ||
) | ||
), | ||
uuids = listOf(UUIDGrant.id(id = chatPamClient.currentUser.id, get = true, update = true, delete = true)) | ||
).await().token | ||
|
||
// client uses token retrieved from server | ||
chatPamClient.pubNub.setToken(token) | ||
|
||
chatPamClient.listenForEvents(channelId) { event: Event<EventContent.Receipt> -> | ||
numberOfReceiptEvents++ | ||
} | ||
|
||
val channel = chatPamClient.createChannel(channelId).await() | ||
delayInMillis(1000) | ||
val membership1 = channel.join().await().membership | ||
membership1.setLastReadMessage( | ||
MessageImpl( | ||
chatPamClient, | ||
timetoken, | ||
EventContent.TextMessageContent("abc"), | ||
channelId = channel.id, | ||
userId = someUser.id | ||
) | ||
).await() | ||
delayInMillis(1000) | ||
assertEquals(2, numberOfReceiptEvents) // join and setLastReadMessage sets LastReadMessageTimetoken | ||
|
||
chatPamServer.deleteChannel(channelId).await() | ||
} | ||
} |
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
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
Submodule pubnub-kotlin
updated
24 files
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