Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added validation for Leave and Heartbeat #326

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ enum class PubNubError(private val code: Int, val message: String) {
180,
"UserId can't be different from UserId in configuration when flag withHeartbeat is set to true",
),

CHANNEL_AND_GROUP_CONTAINS_EMPTY_STRING(
181,
"Channel and/or ChannelGroup contains empty string which is not allowed.",
),
;

override fun toString(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ class HeartbeatEndpoint internal constructor(

override fun validateParams() {
super.validateParams()
if (channels.isEmpty() && channelGroups.isEmpty()) {
throw PubNubException(PubNubError.CHANNEL_AND_GROUP_MISSING)
when {
channels.any { it.isEmpty() } || channelGroups.any { it.isEmpty() } -> {
throw PubNubException(PubNubError.CHANNEL_AND_GROUP_CONTAINS_EMPTY_STRING)
}
channels.isEmpty() && channelGroups.isEmpty() -> {
throw PubNubException(PubNubError.CHANNEL_AND_GROUP_MISSING)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ class LeaveEndpoint internal constructor(pubnub: PubNubImpl) : EndpointCore<Void

override fun validateParams() {
super.validateParams()
if (channels.isEmpty() && channelGroups.isEmpty()) {
throw PubNubException(PubNubError.CHANNEL_AND_GROUP_MISSING)
when {
channels.any { it.isEmpty() } || channelGroups.any { it.isEmpty() } -> {
throw PubNubException(PubNubError.CHANNEL_AND_GROUP_CONTAINS_EMPTY_STRING)
}
channels.isEmpty() && channelGroups.isEmpty() -> {
throw PubNubException(PubNubError.CHANNEL_AND_GROUP_MISSING)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,6 @@ class HeartbeatCoreEndpointTest : BaseTest() {
config.presenceTimeout = 123
// initPubNub()

stubFor(
get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/ch1/heartbeat"))
.willReturn(
aResponse().withBody(
"""
{
"status": 200,
"message": "OK",
"service": "Presence"
}
""".trimIndent(),
),
),
)

try {
HeartbeatEndpoint(pubnub).sync()
failTest()
Expand All @@ -168,6 +153,36 @@ class HeartbeatCoreEndpointTest : BaseTest() {
}
}

@Test
fun testChannelListContainsOnlyEmptyStringSync() {
config.presenceTimeout = 123

try {
HeartbeatEndpoint(pubnub, channels = listOf("", "channel1")).sync()
failTest()
} catch (e: Exception) {
assertPnException(
PubNubError.CHANNEL_AND_GROUP_CONTAINS_EMPTY_STRING,
e,
)
}
}

@Test
fun testChannelGroupListContainsOnlyEmptyStringSync() {
config.presenceTimeout = 123

try {
HeartbeatEndpoint(pubnub, channelGroups = listOf("", "channelGroup1")).sync()
failTest()
} catch (e: Exception) {
assertPnException(
PubNubError.CHANNEL_AND_GROUP_CONTAINS_EMPTY_STRING,
e,
)
}
}

@Test
fun testIsAuthRequiredSuccessSync() {
stubFor(
Expand Down Expand Up @@ -198,23 +213,7 @@ class HeartbeatCoreEndpointTest : BaseTest() {
@Test
fun testBlankSubKeySync() {
config.presenceTimeout = 123

stubFor(
get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/ch1/heartbeat"))
.willReturn(
aResponse().withBody(
"""
{
"status": 200,
"message": "OK",
"service": "Presence"
}
""".trimIndent(),
),
),
)
config.subscribeKey = " "
// initPubNub()

try {
HeartbeatEndpoint(pubnub, listOf("ch1")).sync()
Expand All @@ -230,23 +229,7 @@ class HeartbeatCoreEndpointTest : BaseTest() {
@Test
fun testEmptySubKeySync() {
config.presenceTimeout = 123

stubFor(
get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/ch1/heartbeat"))
.willReturn(
aResponse().withBody(
"""
{
"status": 200,
"message": "OK",
"service": "Presence"
}
""".trimIndent(),
),
),
)
config.subscribeKey = ""
// initPubNub()

try {
HeartbeatEndpoint(pubnub, listOf("ch1")).sync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,6 @@ class LeaveTest : BaseTest() {

@Test
fun testMissingChannelAndGroupSync() {
stubFor(
get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/coolChannel/leave"))
.willReturn(
aResponse().withBody(
"""
{
"status": 200,
"message": "OK",
"service": "Presence",
"action": "leave"
}
""".trimIndent(),
),
),
)

try {
LeaveEndpoint(pubnub).apply {}.sync()
} catch (e: Exception) {
Expand All @@ -214,22 +198,6 @@ class LeaveTest : BaseTest() {

@Test
fun testBlankSubKeySync() {
stubFor(
get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/coolChannel/leave"))
.willReturn(
aResponse().withBody(
"""
{
"status": 200,
"message": "OK",
"service": "Presence",
"action": "leave"
}
""".trimIndent(),
),
),
)

config.subscribeKey = " "

try {
Expand All @@ -241,22 +209,6 @@ class LeaveTest : BaseTest() {

@Test
fun testEmptySubKeySync() {
stubFor(
get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/coolChannel/leave"))
.willReturn(
aResponse().withBody(
"""
{
"status": 200,
"message": "OK",
"service": "Presence",
"action": "leave"
}
""".trimIndent(),
),
),
)

config.subscribeKey = ""

try {
Expand Down Expand Up @@ -294,4 +246,26 @@ class LeaveTest : BaseTest() {
assertEquals(1, requests.size)
assertEquals("myKey", requests[0].queryParameter("auth").firstValue())
}

@Test
fun testChannelListContainsOnlyEmptyStringSync() {
try {
LeaveEndpoint(pubnub).apply {
channels = listOf("", "coolChannel2")
}.sync()
} catch (e: Exception) {
assertPnException(PubNubError.CHANNEL_AND_GROUP_CONTAINS_EMPTY_STRING, e)
}
}

@Test
fun testChannelGroupListContainsOnlyEmptyStringSync() {
try {
LeaveEndpoint(pubnub).apply {
channelGroups = listOf("", "group1")
}.sync()
} catch (e: Exception) {
assertPnException(PubNubError.CHANNEL_AND_GROUP_CONTAINS_EMPTY_STRING, e)
}
}
}
Loading