-
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.
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 instancionate PubNub with new authKey
- Loading branch information
1 parent
c6e9487
commit 24af7d5
Showing
7 changed files
with
155 additions
and
39 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
111 changes: 111 additions & 0 deletions
111
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,111 @@ | ||
package com.pubnub.integration | ||
|
||
import com.pubnub.api.models.consumer.access_manager.v3.ChannelGrant | ||
import com.pubnub.api.models.consumer.access_manager.v3.UUIDGrant | ||
import com.pubnub.api.v2.callbacks.Result | ||
import com.pubnub.chat.Channel | ||
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 | ||
|
||
class AccessManagerTest : BaseChatIntegrationTest() { | ||
@Test | ||
fun pubNubClient_with_PAM_enabled_should_not_getChannel_when_no_token_set() = runTest { | ||
val channelId = channelPam.id | ||
var statusCode = 0 | ||
val expectedStatusCode = 403 // Forbidden, no valid security Token provided | ||
chatPamClient.getChannel(channelId).async { result: Result<Channel?> -> | ||
result.onFailure { e -> | ||
statusCode = e.statusCode | ||
}.onSuccess { channel: Channel? -> | ||
throw IllegalStateException("Should not enter here. Should return exeption") | ||
} | ||
} | ||
delayInMillis(1000) | ||
assertEquals(expectedStatusCode, 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) | ||
|
||
var actualChannelId: String? = "" | ||
val token1 = chatPamClient.pubNub.getToken() | ||
assertEquals(token1, token) | ||
chatPamClient.getChannel(channelId).async { result: Result<Channel?> -> | ||
result.onFailure { | ||
throw IllegalStateException("Should not enter here. Should return channel.") | ||
}.onSuccess { channel: Channel? -> | ||
actualChannelId = channel?.id | ||
} | ||
} | ||
delayInMillis(1000) | ||
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
16 files