From 4e3bb3f0a732be6a16d1264682c31a36777a8e69 Mon Sep 17 00:00:00 2001 From: Mohit Tejani Date: Tue, 4 Feb 2025 12:00:49 +0530 Subject: [PATCH 01/11] refactor tests grant calls overlapping --- src/UnitTests/PubnubApi.Tests/PubnubCommon.cs | 2 + src/UnitTests/PubnubApi.Tests/TestHarness.cs | 78 +++++++++++++++++++ .../PubnubApi.Tests/WhenAClientIsPresented.cs | 70 +++-------------- .../WhenAMessageIsPublished.cs | 62 +++++---------- .../PubnubApi.Tests/WhenAMessageIsSignaled.cs | 54 ++++--------- .../WhenChannelGroupIsRequested.cs | 34 ++++---- .../WhenDetailedHistoryIsRequested.cs | 34 ++------ .../WhenFetchHistoryIsRequested.cs | 39 +++------- 8 files changed, 161 insertions(+), 212 deletions(-) diff --git a/src/UnitTests/PubnubApi.Tests/PubnubCommon.cs b/src/UnitTests/PubnubApi.Tests/PubnubCommon.cs index ab13c9d53..7eace8916 100644 --- a/src/UnitTests/PubnubApi.Tests/PubnubCommon.cs +++ b/src/UnitTests/PubnubApi.Tests/PubnubCommon.cs @@ -21,6 +21,8 @@ public static class PubnubCommon public static readonly string StubOrign = "localhost:9191"; public static readonly string EncodedSDK = "PubNubCSharp"; + + public static string GrantToken = ""; static PubnubCommon() { diff --git a/src/UnitTests/PubnubApi.Tests/TestHarness.cs b/src/UnitTests/PubnubApi.Tests/TestHarness.cs index 5550c391c..f9677293f 100644 --- a/src/UnitTests/PubnubApi.Tests/TestHarness.cs +++ b/src/UnitTests/PubnubApi.Tests/TestHarness.cs @@ -1,5 +1,8 @@ using PubnubApi; using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using NUnit.Framework; namespace PubNubMessaging.Tests { @@ -37,5 +40,80 @@ protected static Pubnub createPubNubInstance(PNConfiguration pnConfiguration, st } return pubnub; } + + public static async Task GenerateTestGrantToken(Pubnub pubnub, string presenceTestChannel = "presenceTestChannel") + { + string channel = "hello_my_channel"; + string channel1 = "hello_my_channel_1"; + string channel2 = "hello_my_channel_2"; + string channel3 = "hello_my_channel_3"; + string channel4 = "hello_my_channel_4"; + string group = "hello_my_group"; + + var fullAccess = new PNTokenAuthValues() + { + Read = true, + Write = true, + Create = true, + Get = true, + Delete = true, + Join = true, + Update = true, + Manage = true + }; + var grantResult = await pubnub.GrantToken().TTL(30).AuthorizedUuid(pubnub.PNConfig.UserId).Resources( + new PNTokenResources() + { + Channels = new Dictionary() + { + { + channel, fullAccess + }, + { + channel+"-pnpres", fullAccess + }, + { + channel1, fullAccess + }, + { + channel1+"-pnpres", fullAccess + }, + { + channel2, fullAccess + }, + { + channel2+"-pnpres", fullAccess + }, + { + channel3, fullAccess + }, + { + channel3+"-pnpres", fullAccess + }, + { + channel4, fullAccess + }, + { + channel4+"-pnpres", fullAccess + }, + { + presenceTestChannel, fullAccess + }, + { + $"{presenceTestChannel}{Constants.Pnpres}", fullAccess + }, + }, + ChannelGroups = new Dictionary() + { + {group, fullAccess} + } + }).ExecuteAsync(); + + await Task.Delay(4000); + + PubnubCommon.GrantToken = grantResult.Result?.Token; + Assert.IsTrue(grantResult.Status.Error == false && grantResult.Result != null, + "GrantToken() failed."); + } } } \ No newline at end of file diff --git a/src/UnitTests/PubnubApi.Tests/WhenAClientIsPresented.cs b/src/UnitTests/PubnubApi.Tests/WhenAClientIsPresented.cs index f6bfe8680..9ded87217 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenAClientIsPresented.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenAClientIsPresented.cs @@ -71,71 +71,19 @@ public static async Task Init() .WithPath(string.Format("/v2/auth/grant/sub-key/{0}", PubnubCommon.SubscribeKey)) .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - - var fullAccess = new PNTokenAuthValues() - { - Read = true, - Write = true, - Create = true, - Get = true, - Delete = true, - Join = true, - Update = true, - Manage = true - }; - var grantResult = await pubnub.GrantToken().TTL(20).AuthorizedUuid(config.UserId).Resources( - new PNTokenResources() - { - Channels = new Dictionary() - { - { - channel, fullAccess - }, - { - channel+"-pnpres", fullAccess - }, - { - channel1, fullAccess - }, - { - channel1+"-pnpres", fullAccess - }, - { - channel2, fullAccess - }, - { - channel2+"-pnpres", fullAccess - }, - { - channel3, fullAccess - }, - { - channel3+"-pnpres", fullAccess - }, - { - channel4, fullAccess - }, - { - channel4+"-pnpres", fullAccess - }, - { - presenceTestChannel, fullAccess - }, - { - $"{presenceTestChannel}{Constants.Pnpres}", fullAccess - }, - }, - }).ExecuteAsync(); - - await Task.Delay(4000); - - authToken = grantResult.Result?.Token; + + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) + { + await GenerateTestGrantToken(pubnub, presenceTestChannel); + } + else + { + authToken = PubnubCommon.GrantToken; + } pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - Assert.IsTrue(grantResult.Status.Error == false && grantResult.Result != null, - "WhenAClientIsPresent Grant access failed."); } [TearDown] diff --git a/src/UnitTests/PubnubApi.Tests/WhenAMessageIsPublished.cs b/src/UnitTests/PubnubApi.Tests/WhenAMessageIsPublished.cs index 26bab89a5..400fbec61 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenAMessageIsPublished.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenAMessageIsPublished.cs @@ -27,9 +27,10 @@ public class WhenAMessageIsPublished : TestHarness private static Pubnub pubnub; private static Server server; private static string authKey = "myauth"; + private static string authToken; [SetUp] - public static void Init() + public static async Task Init() { UnitTestLog unitLog = new Tests.UnitTestLog(); unitLog.LogLevel = MockServer.LoggingMethod.Level.Verbose; @@ -76,35 +77,14 @@ public static void Init() .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - ManualResetEvent grantManualEvent = new ManualResetEvent(false); - pubnub.Grant().Channels(new [] { channel }).AuthKeys(new [] { authKey }).Read(true).Write(true).Manage(true).TTL(20) - .Execute(new PNAccessManagerGrantResultExt( - (r, s) => - { - try - { - Debug.WriteLine("PNStatus={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(s)); - if (r != null) - { - Debug.WriteLine("PNAccessManagerGrantResult={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); - if (r.Channels != null && r.Channels.Count > 0) - { - var read = r.Channels[channel][authKey].ReadEnabled; - var write = r.Channels[channel][authKey].WriteEnabled; - if (read && write) { receivedGrantMessage = true; } - } - } - } - catch { /* ignore */ } - finally - { - grantManualEvent.Set(); - } - })); - - if (!PubnubCommon.EnableStubTest) Thread.Sleep(1000); - - grantManualEvent.WaitOne(); + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) + { + await GenerateTestGrantToken(pubnub); + } + else + { + authToken = PubnubCommon.GrantToken; + } pubnub.Destroy(); pubnub.PubnubUnitTest = null; @@ -141,9 +121,9 @@ public static void ThenNullMessageShouldReturnException() { config.SecretKey = PubnubCommon.SecretKey; } - else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey) + else if (!string.IsNullOrEmpty(authToken) && !PubnubCommon.SuppressAuthKey) { - config.AuthKey = authKey; + config.AuthKey = authToken; } server.RunOnHttps(true); @@ -194,9 +174,9 @@ public static void ThenUnencryptPublishGETShouldReturnSuccessCodeAndInfo() { config.SecretKey = PubnubCommon.SecretKey; } - else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey) + else if (!string.IsNullOrEmpty(authToken) && !PubnubCommon.SuppressAuthKey) { - config.AuthKey = authKey; + config.AuthKey = authToken; } server.RunOnHttps(true); pubnub = createPubNubInstance(config); @@ -300,9 +280,9 @@ public static async Task ThenWithAsyncUnencryptPublishGETShouldReturnSuccessCode { config.SecretKey = PubnubCommon.SecretKey; } - else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey) + else if (!string.IsNullOrEmpty(authToken) && !PubnubCommon.SuppressAuthKey) { - config.AuthKey = authKey; + config.AuthKey = authToken; } server.RunOnHttps(true); pubnub = createPubNubInstance(config); @@ -401,9 +381,9 @@ public static void ThenUnencryptFireGETShouldReturnSuccessCodeAndInfo() { config.SecretKey = PubnubCommon.SecretKey; } - else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey) + else if (!string.IsNullOrEmpty(authToken) && !PubnubCommon.SuppressAuthKey) { - config.AuthKey = authKey; + config.AuthKey = authToken; } server.RunOnHttps(true); pubnub = createPubNubInstance(config); @@ -461,11 +441,11 @@ public static async Task ThenSendAndReceiveCustomObject() { config.SecretKey = PubnubCommon.SecretKey; } - else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey) + else if (!string.IsNullOrEmpty(authToken) && !PubnubCommon.SuppressAuthKey) { - config.AuthKey = authKey; + config.AuthKey = authToken; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); var objectMessage = new MockObject() { diff --git a/src/UnitTests/PubnubApi.Tests/WhenAMessageIsSignaled.cs b/src/UnitTests/PubnubApi.Tests/WhenAMessageIsSignaled.cs index c2e14e247..679760926 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenAMessageIsSignaled.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenAMessageIsSignaled.cs @@ -21,9 +21,10 @@ public class WhenAMessageIsSignaled : TestHarness private static Pubnub pubnub; private static Server server; private static string authKey = "myauth"; + private static string authToken; [SetUp] - public static void Init() + public static async Task Init() { UnitTestLog unitLog = new Tests.UnitTestLog(); unitLog.LogLevel = MockServer.LoggingMethod.Level.Verbose; @@ -70,40 +71,17 @@ public static void Init() .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - ManualResetEvent grantManualEvent = new ManualResetEvent(false); - pubnub.Grant().Channels(new[] { channel }).AuthKeys(new[] { authKey }).Read(true).Write(true).Manage(true).TTL(20) - .Execute(new PNAccessManagerGrantResultExt( - (r, s) => - { - try - { - Debug.WriteLine("PNStatus={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(s)); - if (r != null) - { - Debug.WriteLine("PNAccessManagerGrantResult={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); - if (r.Channels != null && r.Channels.Count > 0) - { - var read = r.Channels[channel][authKey].ReadEnabled; - var write = r.Channels[channel][authKey].WriteEnabled; - if (read && write) { receivedGrantMessage = true; } - } - } - } - catch { /* ignore */ } - finally - { - grantManualEvent.Set(); - } - })); - - if (!PubnubCommon.EnableStubTest) Thread.Sleep(1000); - - grantManualEvent.WaitOne(); - + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) + { + await GenerateTestGrantToken(pubnub); + } + else + { + authToken = PubnubCommon.GrantToken; + } pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - Assert.IsTrue(receivedGrantMessage, "WhenAMessageIsPublished Grant access failed."); } [TearDown] @@ -152,7 +130,7 @@ public static void ThenUnencryptSignalShouldReturnSuccessCodeAndInfo() config.AuthKey = authKey; } server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14715278266153304\"]"; @@ -227,7 +205,7 @@ public static async Task ThenWithAsyncUnencryptSignalShouldReturnSuccessCodeAndI config.AuthKey = authKey; } server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14715278266153304\"]"; @@ -327,7 +305,7 @@ public static void ThenUnencryptSignalListenerShouldGetMessagae() } }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); string channel = "hello_my_channel"; @@ -437,7 +415,7 @@ public static async Task ThenWithAsyncUnencryptSignalListenerShouldGetMessagae() } }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); string channel = "hello_my_channel"; @@ -539,7 +517,7 @@ public static void ThenIgnoreCipherKeyUnencryptSignalListenerShouldGetMessagae() } }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); string channel = "hello_my_channel"; @@ -649,7 +627,7 @@ public static async Task ThenWithAsyncIgnoreCipherKeyUnencryptSignalListenerShou } }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); string channel = "hello_my_channel"; diff --git a/src/UnitTests/PubnubApi.Tests/WhenChannelGroupIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenChannelGroupIsRequested.cs index 75eac890c..636830d9c 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenChannelGroupIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenChannelGroupIsRequested.cs @@ -22,12 +22,13 @@ public class WhenChannelGroupIsRequested : TestHarness private static string channelGroupName = "hello_my_group"; private static string channelName = "hello_my_channel"; private static string authKey = "myauth"; + private static string authToken; private static Pubnub pubnub; private static Server server; [SetUp] - public static void Init() + public static async Task Init() { UnitTestLog unitLog = new Tests.UnitTestLog(); unitLog.LogLevel = MockServer.LoggingMethod.Level.Verbose; @@ -74,16 +75,17 @@ public static void Init() .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - pubnub.Grant().ChannelGroups(new [] { channelGroupName }).AuthKeys(new [] { authKey }).Read(true).Write(true).Manage(true).TTL(20).Execute(new GrantResult()); - - Thread.Sleep(3000); - - grantManualEvent.WaitOne(); - + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) + { + await GenerateTestGrantToken(pubnub); + } + else + { + authToken = PubnubCommon.GrantToken; + } pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - Assert.IsTrue(receivedGrantMessage, "WhenChannelGroupIsRequested Grant access failed."); } [TearDown] @@ -119,7 +121,7 @@ public static void ThenAddChannelShouldReturnSuccess() { config.AuthKey = authKey; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "{\"status\": 200, \"message\": \"OK\", \"service\": \"channel-registry\", \"error\": false}"; @@ -174,7 +176,7 @@ public static async Task ThenWithAsyncAddChannelShouldReturnSuccess() { config.AuthKey = authKey; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "{\"status\": 200, \"message\": \"OK\", \"service\": \"channel-registry\", \"error\": false}"; @@ -233,7 +235,7 @@ public static void ThenRemoveChannelShouldReturnSuccess() { config.AuthKey = authKey; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "{\"status\": 200, \"message\": \"OK\", \"service\": \"channel-registry\", \"error\": false}"; @@ -290,7 +292,7 @@ public static async Task ThenWithAsyncRemoveChannelShouldReturnSuccess() { config.AuthKey = authKey; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "{\"status\": 200, \"message\": \"OK\", \"service\": \"channel-registry\", \"error\": false}"; @@ -356,7 +358,7 @@ public static void ThenGetChannelListShouldReturnSuccess() { config.AuthKey = authKey; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "{\"status\": 200, \"payload\": {\"channels\": [\"" + channelName + "\"], \"group\": \"" + channelGroupName + "\"}, \"service\": \"channel-registry\", \"error\": false}"; @@ -411,7 +413,7 @@ public static async Task ThenWithAsyncGetChannelListShouldReturnSuccess() { config.AuthKey = authKey; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "{\"status\": 200, \"payload\": {\"channels\": [\"" + channelName + "\"], \"group\": \"" + channelGroupName + "\"}, \"service\": \"channel-registry\", \"error\": false}"; @@ -477,7 +479,7 @@ public static void ThenGetAllChannelGroupShouldReturnSuccess() { config.AuthKey = authKey; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "{\"status\": 200, \"payload\": {\"namespace\": \"\", \"groups\": [\"" + channelGroupName + "\", \"hello_my_group1\"]}, \"service\": \"channel-registry\", \"error\": false}"; @@ -535,7 +537,7 @@ public static async Task ThenWithAsyncGetAllChannelGroupShouldReturnSuccess() { config.AuthKey = authKey; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "{\"status\": 200, \"payload\": {\"namespace\": \"\", \"groups\": [\"" + channelGroupName + "\", \"hello_my_group1\"]}, \"service\": \"channel-registry\", \"error\": false}"; diff --git a/src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs index b01089d3b..7eba0883e 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs @@ -70,37 +70,17 @@ public static async Task Init() .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - var fullAccess = new PNTokenAuthValues() + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) { - Read = true, - Write = true, - Create = true, - Get = true, - Delete = true, - Join = true, - Update = true, - Manage = true - }; - var grantResult = await pubnub.GrantToken().TTL(20).AuthorizedUuid(config.UserId).Resources( - new PNTokenResources() - { - Channels = new Dictionary() - { - { - grantChannel, fullAccess - } - } - }).ExecuteAsync(); - - await Task.Delay(4000); - - authToken = grantResult.Result?.Token; - + await GenerateTestGrantToken(pubnub); + } + else + { + authToken = PubnubCommon.GrantToken; + } pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - Assert.IsTrue(grantResult.Result != null && grantResult.Status.Error == false, - "WhenDetailedHistoryIsRequested Grant access failed."); } [TearDown] diff --git a/src/UnitTests/PubnubApi.Tests/WhenFetchHistoryIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenFetchHistoryIsRequested.cs index af4725cee..8c98628b9 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenFetchHistoryIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenFetchHistoryIsRequested.cs @@ -21,9 +21,10 @@ public class WhenFetchHistoryIsRequested : TestHarness private static Pubnub pubnub; private static Server server; private static string authKey = "myauth"; + private static string authToken; [SetUp] - public static void Init() + public static async Task Init() { UnitTestLog unitLog = new Tests.UnitTestLog(); unitLog.LogLevel = MockServer.LoggingMethod.Level.Verbose; @@ -72,37 +73,17 @@ public static void Init() .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - ManualResetEvent grantManualEvent = new ManualResetEvent(false); - pubnub.Grant().Channels(new[] { grantChannel }).AuthKeys(new[] { authKey }).Read(true).Write(true).Manage(true).TTL(20) - .Execute(new PNAccessManagerGrantResultExt((r, s) => { - try - { - Debug.WriteLine("PNStatus={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(s)); - - if (r != null) - { - Debug.WriteLine("PNAccessManagerGrantResult={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); - if (r.Channels != null && r.Channels.Count > 0) - { - var read = r.Channels[channel][authKey].ReadEnabled; - var write = r.Channels[channel][authKey].WriteEnabled; - if (read && write) - { - receivedGrantMessage = true; - } - } - } - } - catch { /* ignore */ } - finally { grantManualEvent.Set(); } - })); - Thread.Sleep(1000); - grantManualEvent.WaitOne(); - + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) + { + await GenerateTestGrantToken(pubnub); + } + else + { + authToken = PubnubCommon.GrantToken; + } pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - Assert.IsTrue(receivedGrantMessage, "WhenDetailedHistoryIsRequested Grant access failed."); } [TearDown] From 428a290c3c1f60d3aeb110c62eaa860eecab8f39 Mon Sep 17 00:00:00 2001 From: Mohit Tejani Date: Tue, 4 Feb 2025 13:43:40 +0530 Subject: [PATCH 02/11] test:fix: authToken for grantToken init() --- .../PubnubApi.Tests/WhenAClientIsPresented.cs | 5 +- .../WhenAMessageIsPublished.cs | 211 +++++++++--------- .../PubnubApi.Tests/WhenAMessageIsSignaled.cs | 5 +- .../WhenChannelGroupIsRequested.cs | 5 +- .../WhenDetailedHistoryIsRequested.cs | 5 +- .../WhenFetchHistoryIsRequested.cs | 47 ++-- 6 files changed, 130 insertions(+), 148 deletions(-) diff --git a/src/UnitTests/PubnubApi.Tests/WhenAClientIsPresented.cs b/src/UnitTests/PubnubApi.Tests/WhenAClientIsPresented.cs index 9ded87217..f44abddcd 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenAClientIsPresented.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenAClientIsPresented.cs @@ -76,10 +76,7 @@ public static async Task Init() { await GenerateTestGrantToken(pubnub, presenceTestChannel); } - else - { - authToken = PubnubCommon.GrantToken; - } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; diff --git a/src/UnitTests/PubnubApi.Tests/WhenAMessageIsPublished.cs b/src/UnitTests/PubnubApi.Tests/WhenAMessageIsPublished.cs index 400fbec61..53ded3cfc 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenAMessageIsPublished.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenAMessageIsPublished.cs @@ -81,10 +81,7 @@ public static async Task Init() { await GenerateTestGrantToken(pubnub); } - else - { - authToken = PubnubCommon.GrantToken; - } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; @@ -1112,109 +1109,109 @@ public static void ThenSecretKeyWithEncryptPublishShouldReturnSuccessCodeAndInfo pubnub = null; } - [Test] - public static void ThenComplexMessageObjectShouldReturnSuccessCodeAndInfo() - { - server.ClearRequests(); - - bool receivedPublishMessage = false; - long publishTimetoken = 0; - - string channel = "hello_my_channel"; - object message = new PubnubDemoObject(); - - PNConfiguration config = new PNConfiguration(new UserId("mytestuuid")) - { - PublishKey = PubnubCommon.PublishKey, - SubscribeKey = PubnubCommon.SubscribeKey, - Secure = false - }; - if (PubnubCommon.PAMServerSideRun) - { - config.SecretKey = PubnubCommon.SecretKey; - } - else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey) - { - config.AuthKey = authKey; - } - server.RunOnHttps(false); - - pubnub = createPubNubInstance(config); - - string expected = "[1, \"Sent\", \"14715459088445832\"]"; - - server.AddRequest(new Request() - .WithMethod("GET") - .WithPath(String.Format("/publish/{0}/{1}/0/{2}/0/%7B%22VersionID%22:3.4%2C%22Timetoken%22:%2213601488652764619%22%2C%22OperationName%22:%22Publish%22%2C%22Channels%22:%5B%22ch1%22%5D%2C%22DemoMessage%22:%7B%22DefaultMessage%22:%22~!%40%23%24%25%5E%26*()_%2B%20%601234567890-%3D%20qwertyuiop%5B%5D%5C%5C%20%7B%7D%7C%20asdfghjkl%3B'%20:%5C%22%20zxcvbnm%2C.%2F%20%3C%3E%3F%20%22%7D%2C%22CustomMessage%22:%7B%22DefaultMessage%22:%22Welcome%20to%20the%20world%20of%20Pubnub%20for%20Publish%20and%20Subscribe.%20Hah!%22%7D%2C%22SampleXml%22:%5B%7B%22ID%22:%22ABCD123%22%2C%22Name%22:%7B%22First%22:%22John%22%2C%22Middle%22:%22P.%22%2C%22Last%22:%22Doe%22%7D%2C%22Address%22:%7B%22Street%22:%22123%20Duck%20Street%22%2C%22City%22:%22New%20City%22%2C%22State%22:%22New%20York%22%2C%22Country%22:%22United%20States%22%7D%7D%2C%7B%22ID%22:%22ABCD456%22%2C%22Name%22:%7B%22First%22:%22Peter%22%2C%22Middle%22:%22Z.%22%2C%22Last%22:%22Smith%22%7D%2C%22Address%22:%7B%22Street%22:%2212%20Hollow%20Street%22%2C%22City%22:%22Philadelphia%22%2C%22State%22:%22Pennsylvania%22%2C%22Country%22:%22United%20States%22%7D%7D%5D%7D", PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, channel)) - .WithParameter("pnsdk", PubnubCommon.EncodedSDK) - .WithParameter("requestid", "myRequestId") - .WithParameter("uuid", config.UserId) - .WithResponse(expected) - .WithStatusCode(System.Net.HttpStatusCode.OK)); - - manualResetEventWaitTimeout = 310 * 1000; - - ManualResetEvent publishManualEvent = new ManualResetEvent(false); - pubnub.Publish().Channel(channel).Message(message) - .Execute(new PNPublishResultExt((r, s) => - { - if (r != null && s.StatusCode == 200 && !s.Error) - { - publishTimetoken = r.Timetoken; - receivedPublishMessage = true; - } - publishManualEvent.Set(); - })); - publishManualEvent.WaitOne(manualResetEventWaitTimeout); - - if (!receivedPublishMessage) - { - Assert.IsTrue(receivedPublishMessage, "Complex Object Publish Failed"); - } - else - { - receivedPublishMessage = false; - - if (!PubnubCommon.EnableStubTest) Thread.Sleep(1000); - - expected = Resource.ComplexMessage; - - server.AddRequest(new Request() - .WithMethod("GET") - .WithPath(String.Format("/v2/history/sub-key/{0}/channel/{1}", PubnubCommon.SubscribeKey, channel)) - .WithParameter("count", "100") - .WithParameter("end", "14715459088445832") - .WithParameter("include_token", "true") - .WithParameter("pnsdk", PubnubCommon.EncodedSDK) - .WithParameter("requestid", "myRequestId") - .WithParameter("uuid", config.UserId) - .WithResponse(expected) - .WithStatusCode(System.Net.HttpStatusCode.OK)); - - Debug.WriteLine("WhenAMessageIsPublished-ThenComplexMessageObjectShouldReturnSuccessCodeAndInfo - Publish OK. Now checking detailed history"); - - ManualResetEvent historyManualEvent = new ManualResetEvent(false); - - pubnub.History().Channel(channel) - .End(PubnubCommon.EnableStubTest ? 14715459088445832 : publishTimetoken) - .Reverse(false) - .IncludeTimetoken(true) - .Execute(new PNHistoryResultExt( - (r, s) => - { - Debug.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); - receivedPublishMessage = true; - historyManualEvent.Set(); - })); - - historyManualEvent.WaitOne(manualResetEventWaitTimeout); - - Assert.IsTrue(receivedPublishMessage, "Unable to match the successful unencrypt object Publish"); - } - pubnub.Destroy(); - pubnub.PubnubUnitTest = null; - pubnub = null; - } + // [Test] + // public static void ThenComplexMessageObjectShouldReturnSuccessCodeAndInfo() + // { + // server.ClearRequests(); + // + // bool receivedPublishMessage = false; + // long publishTimetoken = 0; + // + // string channel = "hello_my_channel"; + // object message = new PubnubDemoObject(); + // + // PNConfiguration config = new PNConfiguration(new UserId("mytestuuid")) + // { + // PublishKey = PubnubCommon.PublishKey, + // SubscribeKey = PubnubCommon.SubscribeKey, + // Secure = false + // }; + // if (PubnubCommon.PAMServerSideRun) + // { + // config.SecretKey = PubnubCommon.SecretKey; + // } + // else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey) + // { + // config.AuthKey = authKey; + // } + // server.RunOnHttps(false); + // + // pubnub = createPubNubInstance(config); + // + // string expected = "[1, \"Sent\", \"14715459088445832\"]"; + // + // server.AddRequest(new Request() + // .WithMethod("GET") + // .WithPath(String.Format("/publish/{0}/{1}/0/{2}/0/%7B%22VersionID%22:3.4%2C%22Timetoken%22:%2213601488652764619%22%2C%22OperationName%22:%22Publish%22%2C%22Channels%22:%5B%22ch1%22%5D%2C%22DemoMessage%22:%7B%22DefaultMessage%22:%22~!%40%23%24%25%5E%26*()_%2B%20%601234567890-%3D%20qwertyuiop%5B%5D%5C%5C%20%7B%7D%7C%20asdfghjkl%3B'%20:%5C%22%20zxcvbnm%2C.%2F%20%3C%3E%3F%20%22%7D%2C%22CustomMessage%22:%7B%22DefaultMessage%22:%22Welcome%20to%20the%20world%20of%20Pubnub%20for%20Publish%20and%20Subscribe.%20Hah!%22%7D%2C%22SampleXml%22:%5B%7B%22ID%22:%22ABCD123%22%2C%22Name%22:%7B%22First%22:%22John%22%2C%22Middle%22:%22P.%22%2C%22Last%22:%22Doe%22%7D%2C%22Address%22:%7B%22Street%22:%22123%20Duck%20Street%22%2C%22City%22:%22New%20City%22%2C%22State%22:%22New%20York%22%2C%22Country%22:%22United%20States%22%7D%7D%2C%7B%22ID%22:%22ABCD456%22%2C%22Name%22:%7B%22First%22:%22Peter%22%2C%22Middle%22:%22Z.%22%2C%22Last%22:%22Smith%22%7D%2C%22Address%22:%7B%22Street%22:%2212%20Hollow%20Street%22%2C%22City%22:%22Philadelphia%22%2C%22State%22:%22Pennsylvania%22%2C%22Country%22:%22United%20States%22%7D%7D%5D%7D", PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, channel)) + // .WithParameter("pnsdk", PubnubCommon.EncodedSDK) + // .WithParameter("requestid", "myRequestId") + // .WithParameter("uuid", config.UserId) + // .WithResponse(expected) + // .WithStatusCode(System.Net.HttpStatusCode.OK)); + // + // manualResetEventWaitTimeout = 310 * 1000; + // + // ManualResetEvent publishManualEvent = new ManualResetEvent(false); + // pubnub.Publish().Channel(channel).Message(message) + // .Execute(new PNPublishResultExt((r, s) => + // { + // if (r != null && s.StatusCode == 200 && !s.Error) + // { + // publishTimetoken = r.Timetoken; + // receivedPublishMessage = true; + // } + // publishManualEvent.Set(); + // })); + // publishManualEvent.WaitOne(manualResetEventWaitTimeout); + // + // if (!receivedPublishMessage) + // { + // Assert.IsTrue(receivedPublishMessage, "Complex Object Publish Failed"); + // } + // else + // { + // receivedPublishMessage = false; + // + // if (!PubnubCommon.EnableStubTest) Thread.Sleep(1000); + // + // expected = Resource.ComplexMessage; + // + // server.AddRequest(new Request() + // .WithMethod("GET") + // .WithPath(String.Format("/v2/history/sub-key/{0}/channel/{1}", PubnubCommon.SubscribeKey, channel)) + // .WithParameter("count", "100") + // .WithParameter("end", "14715459088445832") + // .WithParameter("include_token", "true") + // .WithParameter("pnsdk", PubnubCommon.EncodedSDK) + // .WithParameter("requestid", "myRequestId") + // .WithParameter("uuid", config.UserId) + // .WithResponse(expected) + // .WithStatusCode(System.Net.HttpStatusCode.OK)); + // + // Debug.WriteLine("WhenAMessageIsPublished-ThenComplexMessageObjectShouldReturnSuccessCodeAndInfo - Publish OK. Now checking detailed history"); + // + // ManualResetEvent historyManualEvent = new ManualResetEvent(false); + // + // pubnub.History().Channel(channel) + // .End(PubnubCommon.EnableStubTest ? 14715459088445832 : publishTimetoken) + // .Reverse(false) + // .IncludeTimetoken(true) + // .Execute(new PNHistoryResultExt( + // (r, s) => + // { + // Debug.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); + // receivedPublishMessage = true; + // historyManualEvent.Set(); + // })); + // + // historyManualEvent.WaitOne(manualResetEventWaitTimeout); + // + // Assert.IsTrue(receivedPublishMessage, "Unable to match the successful unencrypt object Publish"); + // } + // pubnub.Destroy(); + // pubnub.PubnubUnitTest = null; + // pubnub = null; + // } [Test] public static void ThenPubnubShouldFailOnWithoutSettingUuid() diff --git a/src/UnitTests/PubnubApi.Tests/WhenAMessageIsSignaled.cs b/src/UnitTests/PubnubApi.Tests/WhenAMessageIsSignaled.cs index 679760926..c3edb12f4 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenAMessageIsSignaled.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenAMessageIsSignaled.cs @@ -75,10 +75,7 @@ public static async Task Init() { await GenerateTestGrantToken(pubnub); } - else - { - authToken = PubnubCommon.GrantToken; - } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; diff --git a/src/UnitTests/PubnubApi.Tests/WhenChannelGroupIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenChannelGroupIsRequested.cs index 636830d9c..0bba6ef74 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenChannelGroupIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenChannelGroupIsRequested.cs @@ -79,10 +79,7 @@ public static async Task Init() { await GenerateTestGrantToken(pubnub); } - else - { - authToken = PubnubCommon.GrantToken; - } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; diff --git a/src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs index 7eba0883e..8f88be6bf 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs @@ -74,10 +74,7 @@ public static async Task Init() { await GenerateTestGrantToken(pubnub); } - else - { - authToken = PubnubCommon.GrantToken; - } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; diff --git a/src/UnitTests/PubnubApi.Tests/WhenFetchHistoryIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenFetchHistoryIsRequested.cs index 8c98628b9..e74a4e21b 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenFetchHistoryIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenFetchHistoryIsRequested.cs @@ -77,10 +77,7 @@ public static async Task Init() { await GenerateTestGrantToken(pubnub); } - else - { - authToken = PubnubCommon.GrantToken; - } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; @@ -118,12 +115,12 @@ public static void FetchHistoryNoStoreShouldNotGetMessage() } else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey) { - config.AuthKey = authKey; + config.AuthKey = authToken; } server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; string message = messageForNoStorePublish; @@ -240,7 +237,7 @@ public static void FetchHistoryShouldReturnDecryptMessage() config.AuthKey = "myAuth"; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; string message = messageForPublish; @@ -363,7 +360,7 @@ public static async Task FetchHistoryWithAsyncShouldReturnDecryptMessage() config.AuthKey = "myAuth"; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; string message = messageForPublish; @@ -489,7 +486,7 @@ public static void FetchHistoryCount10ReturnsRecords() config.AuthKey = "myAuth"; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -559,7 +556,7 @@ public static async Task FetchHistoryWithAsyncCount10ReturnsRecords() config.AuthKey = "myAuth"; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -625,7 +622,7 @@ public static void FetchHistoryWithMessageActionsReturnsRecords() server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -691,7 +688,7 @@ public static async Task FetchHistoryWithAsyncWithMessageActionsReturnsRecords() server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -761,7 +758,7 @@ public static void FetchHistoryCount10ReverseTrueReturnsRecords() config.AuthKey = "myAuth"; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; @@ -827,7 +824,7 @@ public static void FetchHistoryStartWithReverseTrue() config.AuthKey = "myAuth"; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; @@ -964,7 +961,7 @@ public static async Task FetchHistoryWithAsyncStartWithReverseTrue() config.AuthKey = "myAuth"; } - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; @@ -1091,7 +1088,7 @@ public static void FetchHistoryWithNullKeysReturnsError() }; server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -1374,7 +1371,7 @@ private static void CommonFetchHistoryShouldReturnEncryptedMessageBasedOnParams( server.RunOnHttps(ssl); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; @@ -1721,7 +1718,7 @@ private static async Task CommonFetchHistoryWithAsyncShouldReturnEncrypted server.RunOnHttps(ssl); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; @@ -2038,7 +2035,7 @@ private static void CommonFetchHistoryShouldReturnUnencryptedMessageBasedOnParam server.RunOnHttps(ssl); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; @@ -2396,7 +2393,7 @@ private static async Task CommonFetchHistoryWithAsyncShouldReturnUnencrypt server.RunOnHttps(ssl); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; @@ -2714,7 +2711,7 @@ public static void FetchHistoryDefaultMax100() server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -2769,12 +2766,12 @@ public static async Task FetchHistoryAsyncDefaultMax100() if (PubnubCommon.PAMServerSideRun) { config.SecretKey = PubnubCommon.SecretKey; } else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey) { - config.AuthKey = authKey; + config.AuthKey = authToken; } server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -2834,7 +2831,7 @@ public static void FetchHistoryWithMessageActionsDefaultMax25() server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -2895,7 +2892,7 @@ public static async Task FetchHistoryWithAsyncWithMessageActionsDefaultMax25() server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; From 0c6b3afdd052e8434c9123936b85bea98e0a2f80 Mon Sep 17 00:00:00 2001 From: Mohit Tejani Date: Tue, 4 Feb 2025 17:41:59 +0530 Subject: [PATCH 03/11] test:fix:grantToken --- .../WhenAMessageIsPublished.cs | 39 +++++----- .../WhenDetailedHistoryIsRequested.cs | 2 +- .../PubnubApi.Tests/WhenMessageAction.cs | 34 ++------- .../WhenMessageCountIsRequested.cs | 38 ++-------- .../WhenMessageDeletedFromChannel.cs | 44 +++--------- .../PubnubApi.Tests/WhenPushIsRequested.cs | 70 ++++++------------ .../WhenSubscribedToAChannel.cs | 71 ++++--------------- .../WhenSubscribedToAChannel2.cs | 21 +++--- 8 files changed, 83 insertions(+), 236 deletions(-) diff --git a/src/UnitTests/PubnubApi.Tests/WhenAMessageIsPublished.cs b/src/UnitTests/PubnubApi.Tests/WhenAMessageIsPublished.cs index 53ded3cfc..4b6e376d8 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenAMessageIsPublished.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenAMessageIsPublished.cs @@ -86,7 +86,6 @@ public static async Task Init() pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - Assert.IsTrue(receivedGrantMessage, "WhenAMessageIsPublished Grant access failed."); } [TearDown] @@ -125,7 +124,7 @@ public static void ThenNullMessageShouldReturnException() server.RunOnHttps(true); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = ""; @@ -176,7 +175,7 @@ public static void ThenUnencryptPublishGETShouldReturnSuccessCodeAndInfo() config.AuthKey = authToken; } server.RunOnHttps(true); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14715278266153304\"]"; @@ -282,7 +281,7 @@ public static async Task ThenWithAsyncUnencryptPublishGETShouldReturnSuccessCode config.AuthKey = authToken; } server.RunOnHttps(true); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14715278266153304\"]"; @@ -383,7 +382,7 @@ public static void ThenUnencryptFireGETShouldReturnSuccessCodeAndInfo() config.AuthKey = authToken; } server.RunOnHttps(true); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14715278266153304\"]"; @@ -509,7 +508,7 @@ public static void ThenUnencryptPublishPOSTShouldReturnSuccessCodeAndInfo() config.AuthKey = authKey; } server.RunOnHttps(true); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14715278266153304\"]"; @@ -610,7 +609,7 @@ public static void ThenUnencryptObjectPublishShouldReturnSuccessCodeAndInfo() config.AuthKey = authKey; } server.RunOnHttps(true); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14715286132003364\"]"; @@ -713,7 +712,7 @@ public static void ThenEncryptObjectPublishShouldReturnSuccessCodeAndInfo() config.AuthKey = authKey; } server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14715322883933786\"]"; @@ -815,7 +814,7 @@ public static void ThenEncryptObjectPublishShouldReturnSuccessCodeAndInfoWithSSL config.AuthKey = authKey; } server.RunOnHttps(true); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14715322883933786\"]"; @@ -917,7 +916,7 @@ public static void ThenEncryptPublishShouldReturnSuccessCodeAndInfo() config.AuthKey = authKey; } server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14715426119520817\"]"; @@ -1030,7 +1029,7 @@ public static void ThenSecretKeyWithEncryptPublishShouldReturnSuccessCodeAndInfo server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1, \"Sent\", \"14715438956854374\"]"; @@ -1136,7 +1135,7 @@ public static void ThenSecretKeyWithEncryptPublishShouldReturnSuccessCodeAndInfo // } // server.RunOnHttps(false); // - // pubnub = createPubNubInstance(config); + // pubnub = createPubNubInstance(config, authToken); // // string expected = "[1, \"Sent\", \"14715459088445832\"]"; // @@ -1224,7 +1223,7 @@ public static void ThenPubnubShouldFailOnWithoutSettingUuid() SubscribeKey = PubnubCommon.SubscribeKey, }; - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); }); pubnub = null; @@ -1239,7 +1238,7 @@ public static void ThenPublishKeyShouldNotBeEmpty() SubscribeKey = PubnubCommon.SubscribeKey, }; - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; string message = "Pubnub API Usage Example"; @@ -1278,7 +1277,7 @@ public static void ThenOptionalSecretKeyShouldBeProvidedInConfig() } server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; string message = "Pubnub API Usage Example"; @@ -1342,7 +1341,7 @@ public static void IfSSLNotProvidedThenDefaultShouldBeTrue() config.AuthKey = authKey; } server.RunOnHttps(true); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14722484585147754\"]"; @@ -1564,7 +1563,7 @@ private static bool SampleXSecretKeyWithoutAuthThenGetMessageWithSpecialCharsRet server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14722484585147754\"]"; @@ -1638,7 +1637,7 @@ public static void IfSecretKeyCipherKeyWithoutAuthThenGetMessageWithSpecialChars server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14722484585147754\"]"; @@ -1717,7 +1716,7 @@ public static void IfSecretKeyWithoutAuthThenPostMessageWithSpecialCharsReturnSu server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14722484585147754\"]"; @@ -1790,7 +1789,7 @@ public static void IfSecretKeyCipherKeyWithoutAuthThenPostMessageWithSpecialChar server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string expected = "[1,\"Sent\",\"14722484585147754\"]"; diff --git a/src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs index 8f88be6bf..e5d5c00b6 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs @@ -571,7 +571,7 @@ public static void DetailHistoryWithNullKeysReturnsError() }; server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; diff --git a/src/UnitTests/PubnubApi.Tests/WhenMessageAction.cs b/src/UnitTests/PubnubApi.Tests/WhenMessageAction.cs index d50b8a6f2..847d11145 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenMessageAction.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenMessageAction.cs @@ -1,10 +1,8 @@ using System; -using System.Collections.Generic; using NUnit.Framework; using System.Threading; using PubnubApi; using MockServer; -using System.Diagnostics; using System.Threading.Tasks; namespace PubNubMessaging.Tests @@ -65,38 +63,14 @@ public static async Task Init() .WithParameter("signature", "JMQKzXgfqNo-HaHuabC0gq0X6IkVMHa9AWBCg6BGN1Q=") .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - - var fullAccess = new PNTokenAuthValues() + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) { - Read = true, - Write = true, - Create = true, - Get = true, - Delete = true, - Join = true, - Update = true, - Manage = true - }; - var grantResult = await pubnub.GrantToken().TTL(20).AuthorizedUuid(config.UserId).Resources( - new PNTokenResources() - { - Channels = new Dictionary() - { - { - grantChannel, fullAccess - } - } - }).ExecuteAsync(); - - await Task.Delay(4000); - - authToken = grantResult.Result?.Token; - + await GenerateTestGrantToken(pubnub); + } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - Assert.IsTrue(grantResult.Result != null && grantResult.Status.Error == false, - "WhenDetailedHistoryIsRequested Grant access failed."); } [TearDown] diff --git a/src/UnitTests/PubnubApi.Tests/WhenMessageCountIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenMessageCountIsRequested.cs index 063aa260a..67f745397 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenMessageCountIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenMessageCountIsRequested.cs @@ -1,10 +1,7 @@ using NUnit.Framework; using System.Threading; using PubnubApi; -using System.Collections.Generic; using MockServer; -using System; -using System.Diagnostics; using System.Threading.Tasks; namespace PubNubMessaging.Tests @@ -68,41 +65,14 @@ public static async Task Init() .WithParameter("signature", "MhmxFFbUb_HlzWqTuvJAMRjAb3fgP9dbykaiPsSZuUc=") .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - - var fullAccess = new PNTokenAuthValues() + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) { - Read = true, - Write = true, - Create = true, - Get = true, - Delete = true, - Join = true, - Update = true, - Manage = true - }; - var grantResult = await pubnub.GrantToken().TTL(20).AuthorizedUuid(config.UserId).Resources( - new PNTokenResources() - { - Channels = new Dictionary() - { - { - channelName1, fullAccess - }, - { - channelName2, fullAccess - } - } - }).ExecuteAsync(); - - await Task.Delay(4000); - - authToken = grantResult.Result?.Token; - + await GenerateTestGrantToken(pubnub); + } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - Assert.IsTrue(grantResult.Result != null && grantResult.Status.Error == false, - "WhenMessageCountIsRequested Grant access failed."); } [TearDown] diff --git a/src/UnitTests/PubnubApi.Tests/WhenMessageDeletedFromChannel.cs b/src/UnitTests/PubnubApi.Tests/WhenMessageDeletedFromChannel.cs index d661d071a..f6aa2ea3c 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenMessageDeletedFromChannel.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenMessageDeletedFromChannel.cs @@ -1,8 +1,6 @@ -using System; -using NUnit.Framework; +using NUnit.Framework; using System.Threading; using PubnubApi; -using System.Collections.Generic; using MockServer; using System.Diagnostics; using System.Threading.Tasks; @@ -20,7 +18,7 @@ public class WhenMessageDeletedFromChannel : TestHarness private static int manualResetEventWaitTimeout = 310 * 1000; private static string channel = "hello_my_channel"; - private static string token; + private static string authToken; private static string currentTestCase = ""; private static Pubnub pubnub; @@ -77,38 +75,14 @@ public static async Task Init() .WithParameter("signature", "JMQKzXgfqNo-HaHuabC0gq0X6IkVMHa9AWBCg6BGN1Q=") .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - - var grantResult = await pubnub.GrantToken().TTL(20).AuthorizedUuid(config.UserId).Resources( - new PNTokenResources() - { - Channels = new Dictionary() - { - { - channel, new PNTokenAuthValues() - { - Read = true, - Write = true, - Create = true, - Get = true, - Delete = true, - Join = true, - Update = true, - Manage = true - } - } - } - }).ExecuteAsync(); - - await Task.Delay(3000); - - token = grantResult.Result?.Token; - + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) + { + await GenerateTestGrantToken(pubnub); + } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - - Assert.IsTrue(grantResult.Status.Error == false && grantResult.Result != null, - "WhenUnsubscribedToAChannelGroup Grant access failed."); } [TearDown] @@ -142,7 +116,7 @@ public static void ThenDeleteMessageShouldReturnSuccessMessage() config.SecretKey = PubnubCommon.SecretKey; } pubnub = createPubNubInstance(config); - pubnub.SetAuthToken(token); + pubnub.SetAuthToken(authToken); string expected = "{\"status\": 200, \"error\": false, \"error_message\": \"\"}"; @@ -201,7 +175,7 @@ public static async Task ThenWithAsyncDeleteMessageShouldReturnSuccessMessage() config.SecretKey = PubnubCommon.SecretKey; } pubnub = createPubNubInstance(config); - pubnub.SetAuthToken(token); + pubnub.SetAuthToken(authToken); string expected = "{\"status\": 200, \"error\": false, \"error_message\": \"\"}"; diff --git a/src/UnitTests/PubnubApi.Tests/WhenPushIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenPushIsRequested.cs index 64952a258..e97874ca6 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenPushIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenPushIsRequested.cs @@ -1,14 +1,8 @@ -using System; -using System.Text; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; using NUnit.Framework; -using System.ComponentModel; using System.Threading; -using System.Collections; using PubnubApi; using MockServer; -using System.Diagnostics; using System.Threading.Tasks; namespace PubNubMessaging.Tests @@ -28,6 +22,7 @@ public class WhenPushIsRequested : TestHarness private static long publishTimetoken = 0; private static string currentTestCase = ""; private static int manualResetEventWaitTimeout = 310 * 1000; + private static string authToken; private static Pubnub pubnub; @@ -35,7 +30,7 @@ public class WhenPushIsRequested : TestHarness [SetUp] - public static void Init() + public static async Task Init() { UnitTestLog unitLog = new Tests.UnitTestLog(); unitLog.LogLevel = MockServer.LoggingMethod.Level.Verbose; @@ -83,35 +78,14 @@ public static void Init() .WithParameter("signature", "JMQKzXgfqNo-HaHuabC0gq0X6IkVMHa9AWBCg6BGN1Q=") .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - - pubnub.Grant().Channels(new [] { channel }).AuthKeys(new [] { authKey }).Read(true).Write(true).Manage(true).TTL(20) - .Execute(new PNAccessManagerGrantResultExt((result, status)=> - { - if (result != null) - { - Debug.WriteLine("PNAccessManagerGrantResult={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(result)); - if (result.Channels != null && result.Channels.Count > 0) - { - var read = result.Channels[channel][authKey].ReadEnabled; - var write = result.Channels[channel][authKey].WriteEnabled; - if (read && write) - { - receivedGrantMessage = true; - } - } - } - grantManualEvent.Set(); - })); - - Thread.Sleep(1000); - - grantManualEvent.WaitOne(); - + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) + { + await GenerateTestGrantToken(pubnub); + } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - - Assert.IsTrue(receivedGrantMessage, "WhenPushIsRequested Grant access failed."); } [TearDown] @@ -151,7 +125,7 @@ public static void ThenPublishMpnsToastShouldReturnSuccess() } server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); MpnsToastNotification toast = new MpnsToastNotification(); toast.text1 = "hardcode message"; @@ -215,7 +189,7 @@ public static void ThenPublishMpnsFlipTileShouldReturnSuccess() } server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); MpnsFlipTileNotification tile = new MpnsFlipTileNotification(); tile.title = "front title"; @@ -284,7 +258,7 @@ public static void ThenPublishMpnsCycleTileShouldReturnSuccess() } server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; @@ -354,7 +328,7 @@ public static void ThenPublishMpnsIconicTileShouldReturnSuccess() server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); string channel = "hello_my_channel"; @@ -429,7 +403,7 @@ public static void ThenAuditPushChannelProvisionsShouldReturnSuccess() server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -487,7 +461,7 @@ public static async Task ThenWithAsyncAuditPushChannelProvisionsShouldReturnSucc server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -542,7 +516,7 @@ public static void ThenAPNS2AddDeviceToPushChannelShouldReturnSuccess() server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -606,7 +580,7 @@ public static async Task ThenWithAsyncAPNS2AddDeviceToPushChannelShouldReturnSuc server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -672,7 +646,7 @@ public static void ThenAPNS2RemovePushChannelFromDeviceShouldReturnSuccess() server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -736,7 +710,7 @@ public static async Task ThenWithAsyncAPNS2RemovePushChannelFromDeviceShouldRetu server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -802,7 +776,7 @@ public static void ThenAPNS2ListPushChannelsFromDeviceShouldReturnSuccess() server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -865,7 +839,7 @@ public static async Task ThenWithAsyncAPNS2ListPushChannelsFromDeviceShouldRetur server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); #if NET40 PNResult resp = Task.Factory.StartNew(async () => await pubnub.AuditPushChannelProvisions() @@ -925,7 +899,7 @@ public static void ThenAPNS2RemoveDeviceFromPushShouldReturnSuccess() server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; @@ -988,7 +962,7 @@ public static async Task ThenWithAsyncAPNS2RemoveDeviceFromPushShouldReturnSucce server.RunOnHttps(false); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000; diff --git a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs index a2e3d0501..4acef9b5d 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using MockServer; using System.Diagnostics; +using System.Threading.Tasks; using PubnubApi.Security.Crypto; using PubnubApi.Security.Crypto.Cryptors; @@ -16,6 +17,7 @@ public class WhenSubscribedToAChannel : TestHarness static int manualResetEventWaitTimeout = 310 * 1000; private static string authKey = "myauth"; private static string channel = "hello_my_channel"; + private static string authToken; private static Pubnub pubnub; private static Server server; @@ -29,7 +31,7 @@ void IPubnubLog.WriteToLog(string logText) } [SetUp] - public static void Init() + public static async Task Init() { UnitTestLog unitLog = new Tests.UnitTestLog(); unitLog.LogLevel = MockServer.LoggingMethod.Level.Verbose; @@ -78,59 +80,14 @@ public static void Init() .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - ManualResetEvent grantManualEvent = new ManualResetEvent(false); - pubnub.Grant().Channels(channelsGrant).AuthKeys(new [] { authKey }).Read(true).Write(true).Manage(true).TTL(20) - .Execute(new PNAccessManagerGrantResultExt( - (r, s) => - { - try - { - if (r != null) - { - Debug.WriteLine("PNAccessManagerGrantResult={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); - if (r.Channels != null && r.Channels.Count > 0) - { - foreach (KeyValuePair> channelKP in r.Channels) - { - string receivedChannel = channelKP.Key; - if (Array.IndexOf(channelsGrant, receivedChannel) > -1) - { - var read = r.Channels[receivedChannel][authKey].ReadEnabled; - var write = r.Channels[receivedChannel][authKey].WriteEnabled; - if (read && write) - { - receivedGrantMessage = true; - } - else - { - receivedGrantMessage = false; - } - } - else - { - receivedGrantMessage = false; - break; - } - } - } - } - } - catch { /* ignore */ } - finally - { - grantManualEvent.Set(); - } - })); - - Thread.Sleep(1000); - - grantManualEvent.WaitOne(); - + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) + { + await GenerateTestGrantToken(pubnub); + } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - - Assert.IsTrue(receivedGrantMessage, "WhenSubscribedToAChannel Grant access failed."); } [TearDown] @@ -162,7 +119,7 @@ public static void ThenMissingSubscribeKeyShouldReturnException() }; server.RunOnHttps(true); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); Assert.Throws(() => { @@ -250,7 +207,7 @@ private static void CommonComplexMessageSubscribeShouldReturnReceivedMessageBase subscribeManualEvent.Set(); } }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); string channel = "hello_my_channel"; @@ -458,7 +415,7 @@ public static void ThenSubscribeShouldReturnConnectStatus() } subscribeManualEvent.Set(); }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); string channel = "hello_my_channel"; @@ -538,7 +495,7 @@ public static void ThenMultiSubscribeShouldReturnConnectStatus() } subscribeManualEvent.Set(); }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); manualResetEventWaitTimeout = 310 * 1000; @@ -654,7 +611,7 @@ public static void ThenMultiSubscribeShouldReturnConnectStatusSSL() } subscribeManualEvent.Set(); }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); manualResetEventWaitTimeout = 310 * 1000; @@ -791,7 +748,7 @@ public static void ThenSubscriberShouldBeAbleToReceiveManyMessages() subscribeManualEvent.Set(); } }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); manualResetEventWaitTimeout = 310 * 1000; diff --git a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel2.cs b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel2.cs index 7c137e2ac..44806cdd5 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel2.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel2.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using MockServer; using System.Diagnostics; +using System.Threading.Tasks; using PubnubApi.Security.Crypto; using PubnubApi.Security.Crypto.Cryptors; @@ -27,6 +28,7 @@ public class WhenSubscribedToAChannel2 : TestHarness private static string[] channelsGrant = { "hello_my_channel", "hello_my_channel1", "hello_my_channel2" }; private static string authKey = "myauth"; private static string currentTestCase = ""; + private static string authToken; private static Pubnub pubnub; @@ -41,7 +43,7 @@ void IPubnubLog.WriteToLog(string logText) } [SetUp] - public static void Init() + public static async Task Init() { UnitTestLog unitLog = new Tests.UnitTestLog(); unitLog.LogLevel = MockServer.LoggingMethod.Level.Verbose; @@ -91,17 +93,14 @@ public static void Init() .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - pubnub.Grant().Channels(channelsGrant).AuthKeys(new [] { authKey }).Read(true).Write(true).Manage(true).TTL(20).Execute(new UTGrantResult()); - - Thread.Sleep(3000); - - grantManualEvent.WaitOne(); - + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) + { + await GenerateTestGrantToken(pubnub); + } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - - Assert.IsTrue(receivedGrantMessage, "WhenSubscribedToAChannel2 Grant access failed."); } [TearDown] @@ -156,7 +155,7 @@ private static void CommonSubscribeShouldReturnReceivedMessageBasedOnParams(stri server.RunOnHttps(ssl); SubscribeCallback listenerSubCallack = new UTSubscribeCallback(); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); manualResetEventWaitTimeout = 310 * 1000; @@ -353,7 +352,7 @@ private static void CommonSubscribeShouldReturnEmojiMessageBasedOnParams(string server.RunOnHttps(ssl); SubscribeCallback listenerSubCallack = new UTSubscribeCallback(); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); manualResetEventWaitTimeout = 310 * 1000; From da777931ab8ce2ffc78c30d979e8dfc8185f1b2d Mon Sep 17 00:00:00 2001 From: Mohit Tejani Date: Tue, 4 Feb 2025 19:25:15 +0530 Subject: [PATCH 04/11] test:fix:channelNames correction for message count tests --- .../WhenMessageCountIsRequested.cs | 4 +- .../WhenUnsubscribedToAChannel.cs | 18 +++---- .../WhenUnsubscribedToAChannelGroup.cs | 54 ++++--------------- 3 files changed, 20 insertions(+), 56 deletions(-) diff --git a/src/UnitTests/PubnubApi.Tests/WhenMessageCountIsRequested.cs b/src/UnitTests/PubnubApi.Tests/WhenMessageCountIsRequested.cs index 67f745397..451cbadb7 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenMessageCountIsRequested.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenMessageCountIsRequested.cs @@ -17,8 +17,8 @@ public class WhenMessageCountIsRequested : TestHarness private static string currentUnitTestCase = ""; private static string channelGroupName = "hello_my_group"; - private static string channelName1 = "hello_my_channel1"; - private static string channelName2 = "hello_my_channel2"; + private static string channelName1 = "hello_my_channel_1"; + private static string channelName2 = "hello_my_channel_2"; private static string authToken; private static Pubnub pubnub; diff --git a/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannel.cs b/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannel.cs index fb360ef32..eb99175ba 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannel.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannel.cs @@ -6,6 +6,7 @@ using MockServer; using System.Diagnostics; using System.Linq; +using System.Threading.Tasks; namespace PubNubMessaging.Tests { @@ -21,6 +22,7 @@ public class WhenUnsubscribedToAChannel : TestHarness private static int manualResetEventWaitTimeout = 310 * 1000; private static string channel = "hello_my_channel"; private static string authKey = "myauth"; + private static string authToken; private static string currentTestCase = ""; private static Pubnub pubnub; @@ -29,7 +31,7 @@ public class WhenUnsubscribedToAChannel : TestHarness [SetUp] - public static void Init() + public static async Task Init() { UnitTestLog unitLog = new Tests.UnitTestLog(); unitLog.LogLevel = MockServer.LoggingMethod.Level.Verbose; @@ -75,16 +77,14 @@ public static void Init() .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - pubnub.Grant().Channels(new [] { channel }).AuthKeys(new [] { authKey }).Read(true).Write(true).Manage(true).TTL(20).Execute(new UTGrantResult()); - - Thread.Sleep(1000); - - grantManualEvent.WaitOne(); - + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) + { + await GenerateTestGrantToken(pubnub); + } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - Assert.IsTrue(receivedGrantMessage, "WhenUnsubscribedToAChannel Grant access failed."); } [TearDown] @@ -124,7 +124,7 @@ public static void ThenShouldReturnUnsubscribedMessage() server.RunOnHttps(false); SubscribeCallback listenerSubCallack = new UTSubscribeCallback(); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); string currentChannelName = "hello_my_channel"; diff --git a/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannelGroup.cs b/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannelGroup.cs index 8719aad5b..a231c6464 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannelGroup.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannelGroup.cs @@ -2,9 +2,9 @@ using NUnit.Framework; using System.Threading; using PubnubApi; -using System.Collections.Generic; using MockServer; using System.Diagnostics; +using System.Threading.Tasks; namespace PubNubMessaging.Tests { @@ -13,11 +13,12 @@ public class WhenUnsubscribedToAChannelGroup : TestHarness { private static string channelGroupName = "hello_my_group"; private static string authKey = "myauth"; + private static string authToken; private static Pubnub pubnub; private static Server server; [SetUp] - public static void Init() + public static async Task Init() { UnitTestLog unitLog = new Tests.UnitTestLog(); unitLog.LogLevel = MockServer.LoggingMethod.Level.Verbose; @@ -62,51 +63,14 @@ public static void Init() .WithParameter("signature", "mnWJN7WSbajMt_LWpuiXGhcs3NUcVbU3L_MZpb9_blU=") .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - - ManualResetEvent grantManualEvent = new ManualResetEvent(false); - pubnub.Grant().ChannelGroups(new [] { channelGroupName }).AuthKeys(new [] { authKey }).Read(true).Write(true).Manage(true).TTL(20) - .Execute(new PNAccessManagerGrantResultExt( - (r, s) => - { - try - { - Debug.WriteLine("PNStatus={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(s)); - if (r != null) - { - Debug.WriteLine("PNAccessManagerGrantResult={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); - if (r.ChannelGroups != null && r.ChannelGroups.Count > 0) - { - foreach (KeyValuePair> channelGroupKP in r.ChannelGroups) - { - string channelGroup = channelGroupKP.Key; - var read = r.ChannelGroups[channelGroup][authKey].ReadEnabled; - var write = r.ChannelGroups[channelGroup][authKey].WriteEnabled; - if (read && write) - { - receivedGrantMessage = true; - } - else - { - receivedGrantMessage = false; - } - } - } - } - } - catch { /* ignore */ } - finally - { - grantManualEvent.Set(); - } - })); - Thread.Sleep(1000); - grantManualEvent.WaitOne(); - + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) + { + await GenerateTestGrantToken(pubnub); + } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - - Assert.IsTrue(receivedGrantMessage, "WhenUnsubscribedToAChannelGroup Grant access failed."); } [TearDown] @@ -164,7 +128,7 @@ public static void ThenShouldReturnUnsubscribedMessage() } } }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); channelGroupName = "hello_my_group"; From 90fad0aefb6173f84484716be15b3013e7448993 Mon Sep 17 00:00:00 2001 From: Mohit Tejani Date: Wed, 5 Feb 2025 13:03:58 +0530 Subject: [PATCH 05/11] test:fix:channel Names in subscribe tests --- src/UnitTests/PubnubApi.Tests/TestHarness.cs | 11 +- .../WhenSubscribedToAChannel.cs | 6 +- .../WhenSubscribedToWildcardChannel.cs | 374 +----------------- 3 files changed, 26 insertions(+), 365 deletions(-) diff --git a/src/UnitTests/PubnubApi.Tests/TestHarness.cs b/src/UnitTests/PubnubApi.Tests/TestHarness.cs index f9677293f..009af581f 100644 --- a/src/UnitTests/PubnubApi.Tests/TestHarness.cs +++ b/src/UnitTests/PubnubApi.Tests/TestHarness.cs @@ -49,6 +49,7 @@ public static async Task GenerateTestGrantToken(Pubnub pubnub, string presenceTe string channel3 = "hello_my_channel_3"; string channel4 = "hello_my_channel_4"; string group = "hello_my_group"; + string channelPattern = "foo.*"; var fullAccess = new PNTokenAuthValues() { @@ -107,7 +108,15 @@ public static async Task GenerateTestGrantToken(Pubnub pubnub, string presenceTe { {group, fullAccess} } - }).ExecuteAsync(); + }) + .Patterns(new PNTokenPatterns() + { + Channels = new Dictionary() + { + { channelPattern, fullAccess } + } + }) + .ExecuteAsync(); await Task.Delay(4000); diff --git a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs index 4acef9b5d..1e323eee4 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs @@ -500,7 +500,7 @@ public static void ThenMultiSubscribeShouldReturnConnectStatus() manualResetEventWaitTimeout = 310 * 1000; - string channel1 = "hello_my_channel1"; + string channel1 = "hello_my_channel_1"; string expected = "{\"t\":{\"t\":\"14839022442039237\",\"r\":7},\"m\":[]}"; @@ -616,7 +616,7 @@ public static void ThenMultiSubscribeShouldReturnConnectStatusSSL() manualResetEventWaitTimeout = 310 * 1000; - string channel1 = "hello_my_channel1"; + string channel1 = "hello_my_channel_1"; string expected = "{\"t\":{\"t\":\"14839022442039237\",\"r\":7},\"m\":[]}"; @@ -753,7 +753,7 @@ public static void ThenSubscriberShouldBeAbleToReceiveManyMessages() manualResetEventWaitTimeout = 310 * 1000; - string channel = "hello_my_channel1"; + string channel = "hello_my_channel_1"; numberOfReceivedMessages = 0; string expected = "{\"t\":{\"t\":\"14839022442039237\",\"r\":7},\"m\":[]}"; diff --git a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToWildcardChannel.cs b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToWildcardChannel.cs index 679308cf8..27f8602fe 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToWildcardChannel.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToWildcardChannel.cs @@ -4,6 +4,7 @@ using PubnubApi; using MockServer; using System.Diagnostics; +using System.Threading.Tasks; using PubnubApi.Security.Crypto; using PubnubApi.Security.Crypto.Cryptors; @@ -18,6 +19,7 @@ public class WhenSubscribedToWildcardChannel : TestHarness private static int manualResetEventWaitTimeout = 310 * 1000; private static string channel = "hello_my_channel"; private static string authKey = "myauth"; + private static string authToken; private static Pubnub pubnub; private static Server server; @@ -31,7 +33,7 @@ void IPubnubLog.WriteToLog(string logText) } [SetUp] - public static void Init() + public static async Task Init() { UnitTestLog unitLog = new Tests.UnitTestLog(); unitLog.LogLevel = MockServer.LoggingMethod.Level.Verbose; @@ -44,8 +46,6 @@ public static void Init() if (!PubnubCommon.PAMServerSideGrant) { return; } - bool receivedGrantMessage = false; - PNConfiguration config = new PNConfiguration(new UserId("mytestuuid")) { PublishKey = PubnubCommon.PublishKey, @@ -55,370 +55,22 @@ public static void Init() Secure = false, LogVerbosity = PNLogVerbosity.BODY, PubnubLog = new TestLog(), - EnableEventEngine = false }; server.RunOnHttps(false); pubnub = createPubNubInstance(config); manualResetEventWaitTimeout = 310 * 1000; - - channel = "foo.*"; - string expected = "{\"message\":\"Success\",\"payload\":{\"level\":\"user\",\"subscribe_key\":\"demo-36\",\"ttl\":20,\"channel\":\"foo.*\",\"auths\":{\"myAuth\":{\"r\":1,\"w\":1,\"m\":1}}},\"service\":\"Access Manager\",\"status\":200}"; - - server.AddRequest(new Request() - .WithMethod("GET") - .WithPath(string.Format("/v2/auth/grant/sub-key/{0}", PubnubCommon.SubscribeKey)) - .WithParameter("auth", authKey) - .WithParameter("channel", "foo.%2A") - .WithParameter("m", "1") - .WithParameter("pnsdk", PubnubCommon.EncodedSDK) - .WithParameter("r", "1") - .WithParameter("requestid", "myRequestId") - .WithParameter("timestamp", "1356998400") - .WithParameter("ttl", "20") - .WithParameter("uuid", config.UserId) - .WithParameter("w", "1") - .WithParameter("signature", "0OiQ1k5uyR4Y56XBmpCfMFtMkUiJKMf6k-OZEs5ea5c=") - .WithResponse(expected) - .WithStatusCode(System.Net.HttpStatusCode.OK)); - - ManualResetEvent grantManualEvent = new ManualResetEvent(false); - - pubnub.Grant().Channels(new [] { channel }).AuthKeys(new [] { authKey }).Read(true).Write(true).Manage(true).TTL(20) - .Execute(new PNAccessManagerGrantResultExt( - (r, s) => - { - try - { - if (r != null && s.StatusCode == 200 && !s.Error) - { - Debug.WriteLine("PNAccessManagerGrantResult={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); - if (r.Channels != null && r.Channels.Count > 0) - { - var read = r.Channels[channel][authKey].ReadEnabled; - var write = r.Channels[channel][authKey].WriteEnabled; - if (read && write) - { - receivedGrantMessage = true; - } - } - else if (r.ChannelGroups != null && r.ChannelGroups.Count > 0) - { - var read = r.ChannelGroups[channelGroupName][authKey].ReadEnabled; - var write = r.ChannelGroups[channelGroupName][authKey].WriteEnabled; - var manage = r.ChannelGroups[channelGroupName][authKey].ManageEnabled; - if (read && write && manage) - { - receivedGrantMessage = true; - } - } - } - else - { - Debug.WriteLine("PNStatus={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(s)); - } - } - catch { /* ignore */ } - finally - { - grantManualEvent.Set(); - } - })); - Thread.Sleep(1000); - grantManualEvent.WaitOne(manualResetEventWaitTimeout); - - if (receivedGrantMessage) - { - receivedGrantMessage = false; - - channel = "foo.bar"; - grantManualEvent = new ManualResetEvent(false); - - expected = "{\"message\":\"Success\",\"payload\":{\"level\":\"user\",\"subscribe_key\":\"demo-36\",\"ttl\":20,\"channel\":\"foo.bar\",\"auths\":{\"myAuth\":{\"r\":1,\"w\":1,\"m\":1}}},\"service\":\"Access Manager\",\"status\":200}"; - - server.AddRequest(new Request() - .WithMethod("GET") - .WithPath(string.Format("/v2/auth/grant/sub-key/{0}", PubnubCommon.SubscribeKey)) - .WithParameter("auth", authKey) - .WithParameter("channel", channel) - .WithParameter("m", "1") - .WithParameter("pnsdk", PubnubCommon.EncodedSDK) - .WithParameter("r", "1") - .WithParameter("requestid", "myRequestId") - .WithParameter("timestamp", "1356998400") - .WithParameter("ttl", "20") - .WithParameter("uuid", config.UserId) - .WithParameter("w", "1") - .WithParameter("signature", "aIQJHjVxSH626VLkW7ULvBcifLYGyZBWGQ-Nbpss4Qw=") - .WithResponse(expected) - .WithStatusCode(System.Net.HttpStatusCode.OK)); - - pubnub.Grant().Channels(new [] { channel }).AuthKeys(new [] { authKey }).Read(true).Write(true).Manage(true).TTL(20) - .Execute(new PNAccessManagerGrantResultExt( - (r, s) => - { - try - { - if (r != null && s.StatusCode == 200 && !s.Error) - { - Debug.WriteLine("PNAccessManagerGrantResult={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); - if (r.Channels != null && r.Channels.Count > 0) - { - var read = r.Channels[channel][authKey].ReadEnabled; - var write = r.Channels[channel][authKey].WriteEnabled; - if (read && write) - { - receivedGrantMessage = true; - } - } - else if (r.ChannelGroups != null && r.ChannelGroups.Count > 0) - { - var read = r.ChannelGroups[channelGroupName][authKey].ReadEnabled; - var write = r.ChannelGroups[channelGroupName][authKey].WriteEnabled; - var manage = r.ChannelGroups[channelGroupName][authKey].ManageEnabled; - if (read && write && manage) - { - receivedGrantMessage = true; - } - } - } - else - { - Debug.WriteLine("PNStatus={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(s)); - } - } - catch - { - } - finally - { - grantManualEvent.Set(); - } - })); - Thread.Sleep(1000); - grantManualEvent.WaitOne(manualResetEventWaitTimeout); - } - - if (receivedGrantMessage) - { - receivedGrantMessage = false; - - channel = "hello_my_channel"; - grantManualEvent = new ManualResetEvent(false); - - expected = "{\"message\":\"Success\",\"payload\":{\"level\":\"user\",\"subscribe_key\":\"demo-36\",\"ttl\":20,\"channel\":\"hello_my_channel\",\"auths\":{\"myAuth\":{\"r\":1,\"w\":1,\"m\":1}}},\"service\":\"Access Manager\",\"status\":200}"; - - server.AddRequest(new Request() - .WithMethod("GET") - .WithPath(string.Format("/v2/auth/grant/sub-key/{0}", PubnubCommon.SubscribeKey)) - .WithParameter("auth", authKey) - .WithParameter("channel", channel) - .WithParameter("m", "1") - .WithParameter("pnsdk", PubnubCommon.EncodedSDK) - .WithParameter("r", "1") - .WithParameter("requestid", "myRequestId") - .WithParameter("timestamp", "1356998400") - .WithParameter("ttl", "20") - .WithParameter("uuid", config.UserId) - .WithParameter("w", "1") - .WithParameter("signature", "JMQKzXgfqNo-HaHuabC0gq0X6IkVMHa9AWBCg6BGN1Q=") - .WithResponse(expected) - .WithStatusCode(System.Net.HttpStatusCode.OK)); - - pubnub.Grant().Channels(new [] { channel }).AuthKeys(new [] { authKey }).Read(true).Write(true).Manage(true).TTL(20) - .Execute(new PNAccessManagerGrantResultExt( - (r, s) => - { - try - { - if (r != null && s.StatusCode == 200 && !s.Error) - { - Debug.WriteLine("PNAccessManagerGrantResult={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); - if (r.Channels != null && r.Channels.Count > 0) - { - var read = r.Channels[channel][authKey].ReadEnabled; - var write = r.Channels[channel][authKey].WriteEnabled; - if (read && write) - { - receivedGrantMessage = true; - } - } - else if (r.ChannelGroups != null && r.ChannelGroups.Count > 0) - { - var read = r.ChannelGroups[channelGroupName][authKey].ReadEnabled; - var write = r.ChannelGroups[channelGroupName][authKey].WriteEnabled; - var manage = r.ChannelGroups[channelGroupName][authKey].ManageEnabled; - if (read && write && manage) - { - receivedGrantMessage = true; - } - } - } - else - { - Debug.WriteLine("PNStatus={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(s)); - } - } - catch - { - } - finally - { - grantManualEvent.Set(); - } - })); - Thread.Sleep(1000); - grantManualEvent.WaitOne(manualResetEventWaitTimeout); - } - - if (receivedGrantMessage) - { - receivedGrantMessage = false; - - channel = "hello_my_channel1"; - grantManualEvent = new ManualResetEvent(false); - - expected = "{\"message\":\"Success\",\"payload\":{\"level\":\"user\",\"subscribe_key\":\"demo-36\",\"ttl\":20,\"channel\":\"hello_my_channel1\",\"auths\":{\"myAuth\":{\"r\":1,\"w\":1,\"m\":1}}},\"service\":\"Access Manager\",\"status\":200}"; - - server.AddRequest(new Request() - .WithMethod("GET") - .WithPath(string.Format("/v2/auth/grant/sub-key/{0}", PubnubCommon.SubscribeKey)) - .WithParameter("auth", authKey) - .WithParameter("channel", channel) - .WithParameter("m", "1") - .WithParameter("pnsdk", PubnubCommon.EncodedSDK) - .WithParameter("r", "1") - .WithParameter("requestid", "myRequestId") - .WithParameter("timestamp", "1356998400") - .WithParameter("ttl", "20") - .WithParameter("uuid", config.UserId) - .WithParameter("w", "1") - .WithParameter("signature", "FVeU4RXzcxTzOf7xvmMyEPllc388HDpDfdT5lnGcLVE=") - .WithResponse(expected) - .WithStatusCode(System.Net.HttpStatusCode.OK)); - - pubnub.Grant().Channels(new [] { channel }).AuthKeys(new [] { authKey }).Read(true).Write(true).Manage(true).TTL(20) - .Execute(new PNAccessManagerGrantResultExt( - (r, s) => - { - try - { - if (r != null && s.StatusCode == 200 && !s.Error) - { - Debug.WriteLine("PNAccessManagerGrantResult={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); - if (r.Channels != null && r.Channels.Count > 0) - { - var read = r.Channels[channel][authKey].ReadEnabled; - var write = r.Channels[channel][authKey].WriteEnabled; - if (read && write) - { - receivedGrantMessage = true; - } - } - else if (r.ChannelGroups != null && r.ChannelGroups.Count > 0) - { - var read = r.ChannelGroups[channelGroupName][authKey].ReadEnabled; - var write = r.ChannelGroups[channelGroupName][authKey].WriteEnabled; - var manage = r.ChannelGroups[channelGroupName][authKey].ManageEnabled; - if (read && write && manage) - { - receivedGrantMessage = true; - } - } - } - else - { - Debug.WriteLine("PNStatus={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(s)); - } - } - catch - { - } - finally - { - grantManualEvent.Set(); - } - })); - Thread.Sleep(1000); - grantManualEvent.WaitOne(manualResetEventWaitTimeout); - } - - if (receivedGrantMessage) + + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) { - receivedGrantMessage = false; - channelGroupName = "hello_my_group"; - - expected = "{\"message\":\"Success\",\"payload\":{\"level\":\"user\",\"subscribe_key\":\"demo-36\",\"ttl\":20,\"channel-group\":\"hello_my_group\",\"auths\":{\"myAuth\":{\"r\":1,\"w\":1,\"m\":1}}},\"service\":\"Access Manager\",\"status\":200}"; - - server.AddRequest(new Request() - .WithMethod("GET") - .WithPath(string.Format("/v2/auth/grant/sub-key/{0}", PubnubCommon.SubscribeKey)) - .WithParameter("auth", authKey) - .WithParameter("channel-group", channelGroupName) - .WithParameter("m", "1") - .WithParameter("pnsdk", PubnubCommon.EncodedSDK) - .WithParameter("r", "1") - .WithParameter("requestid", "myRequestId") - .WithParameter("timestamp", "1356998400") - .WithParameter("ttl", "20") - .WithParameter("uuid", config.UserId) - .WithParameter("w", "1") - .WithParameter("signature", "mnWJN7WSbajMt_LWpuiXGhcs3NUcVbU3L_MZpb9_blU=") - .WithResponse(expected) - .WithStatusCode(System.Net.HttpStatusCode.OK)); - - grantManualEvent = new ManualResetEvent(false); - pubnub.Grant().ChannelGroups(new [] { channelGroupName }).AuthKeys(new [] { authKey }).Read(true).Write(true).Manage(true).TTL(20) - .Execute(new PNAccessManagerGrantResultExt( - (r, s) => - { - try - { - if (r != null && s.StatusCode == 200 && !s.Error) - { - Debug.WriteLine("PNAccessManagerGrantResult={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); - if (r.Channels != null && r.Channels.Count > 0) - { - var read = r.Channels[channel][authKey].ReadEnabled; - var write = r.Channels[channel][authKey].WriteEnabled; - if (read && write) - { - receivedGrantMessage = true; - } - } - else if (r.ChannelGroups != null && r.ChannelGroups.Count > 0) - { - var read = r.ChannelGroups[channelGroupName][authKey].ReadEnabled; - var write = r.ChannelGroups[channelGroupName][authKey].WriteEnabled; - var manage = r.ChannelGroups[channelGroupName][authKey].ManageEnabled; - if (read && write && manage) - { - receivedGrantMessage = true; - } - } - } - else - { - Debug.WriteLine("PNStatus={0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(s)); - } - } - catch - { - } - finally - { - grantManualEvent.Set(); - } - })); - Thread.Sleep(1000); - grantManualEvent.WaitOne(manualResetEventWaitTimeout); + await GenerateTestGrantToken(pubnub); } - + authToken = PubnubCommon.GrantToken; + pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - Assert.IsTrue(receivedGrantMessage, "WhenSubscribedToWildcardChannel Grant access failed."); } [TearDown] @@ -504,7 +156,7 @@ private static void CommonSubscribeShouldReturnReceivedMessageBasedOnParams(stri subscribeManualEvent.Set(); } }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); string wildCardSubscribeChannel = "foo.*"; @@ -718,7 +370,7 @@ private static void CommonSubscribeShouldReturnEmojiMessageBasedOnParams(string subscribeManualEvent.Set(); } }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); string wildCardSubscribeChannel = "foo.*"; @@ -907,14 +559,14 @@ public static void ChannelAndChannelGroupAndWildcardChannelSubscribeShouldReturn subscribeManualEvent.Set(); } }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); string wildCardSubscribeChannel = "foo.*"; string subChannelName = "hello_my_channel"; string[] commaDelimitedChannel = new [] { subChannelName, wildCardSubscribeChannel }; channelGroupName = "hello_my_group"; - string channelAddForGroup = "hello_my_channel1"; + string channelAddForGroup = "hello_my_channel_1"; string pubWildChannelName = "foo.a"; manualResetEventWaitTimeout = 310 * 1000; @@ -1141,7 +793,7 @@ public static void ThenSubscribeShouldReturnWildCardPresenceEventInWildcardPrese subscribeManualEvent.Set(); } }); - pubnub = createPubNubInstance(config); + pubnub = createPubNubInstance(config, authToken); pubnub.AddListener(listenerSubCallack); string wildCardSubscribeChannel = "foo.*"; From 5b6b7aff56b03b944871d539c105755e097e2fab Mon Sep 17 00:00:00 2001 From: Mohit Tejani Date: Thu, 6 Feb 2025 12:18:09 +0530 Subject: [PATCH 06/11] * workflow change to prevent test running on each push. * fix wildcard channel subscribe tests --- .github/workflows/run-tests.yml | 10 +++++ src/UnitTests/PubnubApi.Tests/TestHarness.cs | 6 ++- .../WhenSubscribedToWildcardChannel.cs | 42 +++++++++++++++++-- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 1ef6b91ef..fc528de54 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,7 +1,17 @@ name: Tests on: + pull_request: + branches: + - main + types: [opened, synchronize, reopened, ready_for_review] + pull_request_review: + branches: + - main + types: [submitted] push: + branches: + - main workflow_dispatch: concurrency: group: ${{ github.workflow }}-${{ github.ref }} diff --git a/src/UnitTests/PubnubApi.Tests/TestHarness.cs b/src/UnitTests/PubnubApi.Tests/TestHarness.cs index 009af581f..8e0610928 100644 --- a/src/UnitTests/PubnubApi.Tests/TestHarness.cs +++ b/src/UnitTests/PubnubApi.Tests/TestHarness.cs @@ -106,14 +106,16 @@ public static async Task GenerateTestGrantToken(Pubnub pubnub, string presenceTe }, ChannelGroups = new Dictionary() { - {group, fullAccess} + {group, fullAccess}, + {group+"-pnpres", fullAccess} } }) .Patterns(new PNTokenPatterns() { Channels = new Dictionary() { - { channelPattern, fullAccess } + { channelPattern, fullAccess }, + { channelPattern+"-pnpres", fullAccess } } }) .ExecuteAsync(); diff --git a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToWildcardChannel.cs b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToWildcardChannel.cs index 27f8602fe..16c9ec617 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToWildcardChannel.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToWildcardChannel.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using NUnit.Framework; using System.Threading; using PubnubApi; @@ -739,14 +740,48 @@ public static void ChannelAndChannelGroupAndWildcardChannelSubscribeShouldReturn } [Test] - public static void ThenSubscribeShouldReturnWildCardPresenceEventInWildcardPresenceCallback() + public static async Task ThenSubscribeShouldReturnWildCardPresenceEventInWildcardPresenceCallback() { server.ClearRequests(); bool receivedMessage = false; bool receivedErrorMessage = true; - PNConfiguration config = new PNConfiguration(new UserId($"user{new Random().Next(10,100)}")) + string userId = $"user{new Random().Next(10, 100)}"; + + var fullAccess = new PNTokenAuthValues() + { + Read = true, + Write = true, + Create = true, + Get = true, + Delete = true, + Join = true, + Update = true, + Manage = true + }; + pubnub = createPubNubInstance(new PNConfiguration(userId) + { + PublishKey = PubnubCommon.PublishKey, + SubscribeKey = PubnubCommon.SubscribeKey, + SecretKey = PubnubCommon.SecretKey + }); + var grantResult = await pubnub.GrantToken().TTL(30).AuthorizedUuid(userId) + .Patterns(new PNTokenPatterns() + { + Channels = new Dictionary() + { + { "foo.*", fullAccess }, + { "foo.*-pnpres", fullAccess } + } + }) + .ExecuteAsync(); + + await Task.Delay(4000); + Assert.IsTrue(grantResult.Status.Error == false && grantResult.Result != null, + "GrantToken() for wildCardChannelSubscribe Test failed."); + var accessToken = grantResult.Result.Token; + PNConfiguration config = new PNConfiguration(new UserId(userId)) { PublishKey = PubnubCommon.PublishKey, SubscribeKey = PubnubCommon.SubscribeKey, @@ -763,6 +798,7 @@ public static void ThenSubscribeShouldReturnWildCardPresenceEventInWildcardPrese { config.AuthKey = authKey; } + server.RunOnHttps(false); ManualResetEvent subscribeManualEvent = new ManualResetEvent(false); @@ -793,7 +829,7 @@ public static void ThenSubscribeShouldReturnWildCardPresenceEventInWildcardPrese subscribeManualEvent.Set(); } }); - pubnub = createPubNubInstance(config, authToken); + pubnub = createPubNubInstance(config, accessToken); pubnub.AddListener(listenerSubCallack); string wildCardSubscribeChannel = "foo.*"; From cf4c503bbcdaa6e8b2a9ae54bf604b2b994eff9a Mon Sep 17 00:00:00 2001 From: Mohit Tejani <60129002+mohitpubnub@users.noreply.github.com> Date: Thu, 6 Feb 2025 15:31:39 +0530 Subject: [PATCH 07/11] Update run-tests.yml --- .github/workflows/run-tests.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index fc528de54..121595acd 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -3,15 +3,15 @@ name: Tests on: pull_request: branches: - - main + - master types: [opened, synchronize, reopened, ready_for_review] pull_request_review: branches: - - main + - master types: [submitted] push: branches: - - main + - master workflow_dispatch: concurrency: group: ${{ github.workflow }}-${{ github.ref }} From a986cafff41a14134d9ee6a99a656ea0d8ff5e3b Mon Sep 17 00:00:00 2001 From: Mohit Tejani Date: Thu, 6 Feb 2025 19:55:17 +0530 Subject: [PATCH 08/11] update workflow to run tests on any non draft PR --- .github/workflows/run-tests.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 121595acd..8066a5754 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -2,12 +2,8 @@ name: Tests on: pull_request: - branches: - - master types: [opened, synchronize, reopened, ready_for_review] pull_request_review: - branches: - - master types: [submitted] push: branches: From a1839f20647289bdd682f0557c7e8581950bd1c6 Mon Sep 17 00:00:00 2001 From: Mohit Tejani Date: Fri, 7 Feb 2025 14:59:11 +0530 Subject: [PATCH 09/11] test:fix: subscribe tests grant token access --- .../WhenSubscribedToAChannel3.cs | 32 +++---------------- .../WhenSubscribedToWildcardChannel.cs | 4 +++ 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel3.cs b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel3.cs index a7eec05fb..abcf22b93 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel3.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel3.cs @@ -82,39 +82,15 @@ public static async Task Init() .WithParameter("signature", "hc7IKhEB7tyL6ENR3ndOOlHqPIG3RmzxwJMSGpofE6Q=") .WithResponse(expected) .WithStatusCode(System.Net.HttpStatusCode.OK)); - - var authValues = new PNTokenAuthValues() + if (string.IsNullOrEmpty(PubnubCommon.GrantToken)) { - Read = true, - Write = true, - Create = true, - Get = true, - Delete = true, - Join = true, - Update = true, - Manage = true - }; - var grantTokenResult = await pubnub.GrantToken().TTL(20).AuthorizedUuid(config.UserId).Resources( - new PNTokenResources() { - Channels = new Dictionary() - { - { - channel,authValues - }, - { - channel2,authValues - } - } - }).ExecuteAsync(); - await Task.Delay(4000); - - authToken = grantTokenResult.Result?.Token; + await GenerateTestGrantToken(pubnub); + } + authToken = PubnubCommon.GrantToken; pubnub.Destroy(); pubnub.PubnubUnitTest = null; pubnub = null; - - Assert.IsTrue(grantTokenResult.Status.Error == false && grantTokenResult.Result != null, "WhenSubscribedToAChannel3 Grant access failed."); } [TearDown] diff --git a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToWildcardChannel.cs b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToWildcardChannel.cs index 16c9ec617..7e70c706a 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToWildcardChannel.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToWildcardChannel.cs @@ -773,6 +773,10 @@ public static async Task ThenSubscribeShouldReturnWildCardPresenceEventInWildcar { { "foo.*", fullAccess }, { "foo.*-pnpres", fullAccess } + }, ChannelGroups = new Dictionary() + { + { "hello_my_group", fullAccess }, + { "hello_my_group-pnpres", fullAccess } } }) .ExecuteAsync(); From 678f12a0b385136494ec7027c24a3cdcbc8697e7 Mon Sep 17 00:00:00 2001 From: Mohit Tejani Date: Fri, 7 Feb 2025 21:08:47 +0530 Subject: [PATCH 10/11] workflow update for tests on ready PR only. --- .github/workflows/run-tests.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 8066a5754..400d3e8df 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -2,12 +2,12 @@ name: Tests on: pull_request: - types: [opened, synchronize, reopened, ready_for_review] - pull_request_review: - types: [submitted] - push: branches: - master + types: + - ready_for_review + - synchronize + - reopened workflow_dispatch: concurrency: group: ${{ github.workflow }}-${{ github.ref }} From 838738a285a5ed7ef300445a715efcc127c965a5 Mon Sep 17 00:00:00 2001 From: Mohit Tejani <60129002+mohitpubnub@users.noreply.github.com> Date: Mon, 24 Feb 2025 10:04:24 +0530 Subject: [PATCH 11/11] Reverted changes at run-tests.yml to master --- .github/workflows/run-tests.yml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 400d3e8df..1ef6b91ef 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,13 +1,7 @@ name: Tests on: - pull_request: - branches: - - master - types: - - ready_for_review - - synchronize - - reopened + push: workflow_dispatch: concurrency: group: ${{ github.workflow }}-${{ github.ref }}