Skip to content

Commit

Permalink
Change PAM v2 to v3 in more tests, more failing test debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub-grzesiowski committed Sep 24, 2024
1 parent 8976cf5 commit ed038be
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 409 deletions.
4 changes: 2 additions & 2 deletions src/UnitTests/PubnubApi.Tests/PubnubCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ static PubnubCommon()
{
}

public static string TEMP_DumpInfo()
public static string TEMP_DebugDump()
{
return $"EnvPAMServerSideRun: {EnvPAMServerSideRun}, EnvPublishKey: {EnvPublishKey}, EnvSubscribeKey: {EnvSubscribeKey}, EnvSecretKey: {EnvSecretKey}";
return $"EnvPAMServerSideRun: {EnvPAMServerSideRun}, EnvPublishKey: {EnvPublishKey[^4]}, EnvSubscribeKey: {EnvSubscribeKey[^4]}, EnvSecretKey: {EnvSecretKey[^4]}";
}
}
}
6 changes: 5 additions & 1 deletion src/UnitTests/PubnubApi.Tests/TestHarness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace PubNubMessaging.Tests
{
public class TestHarness
{
protected static Pubnub createPubNubInstance(PNConfiguration pnConfiguration)
protected static Pubnub createPubNubInstance(PNConfiguration pnConfiguration, string authToken = "")
{
Pubnub pubnub = null;
if (PubnubCommon.EnableStubTest)
Expand All @@ -31,6 +31,10 @@ protected static Pubnub createPubNubInstance(PNConfiguration pnConfiguration)
pnConfiguration.Origin = "ps.pndsn.com";
pubnub = new Pubnub(pnConfiguration);
}
if (!string.IsNullOrEmpty(authToken))
{
pubnub.SetAuthToken(authToken);
}
return pubnub;
}
}
Expand Down
201 changes: 58 additions & 143 deletions src/UnitTests/PubnubApi.Tests/WhenAClientIsPresented.cs

Large diffs are not rendered by default.

95 changes: 34 additions & 61 deletions src/UnitTests/PubnubApi.Tests/WhenDetailedHistoryIsRequested.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,10 +20,10 @@ public class WhenDetailedHistoryIsRequested : TestHarness
private static int manualResetEventWaitTimeout = 310 * 1000;
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;
Expand All @@ -42,7 +43,6 @@ public static void Init()
PublishKey = PubnubCommon.PublishKey,
SubscribeKey = PubnubCommon.SubscribeKey,
SecretKey = PubnubCommon.SecretKey,
AuthKey = authKey,
Secure = false
};
server.RunOnHttps(false);
Expand All @@ -56,7 +56,7 @@ public static void Init()
server.AddRequest(new Request()
.WithMethod("GET")
.WithPath(string.Format("/v2/auth/grant/sub-key/{0}", PubnubCommon.SubscribeKey))
.WithParameter("auth", authKey)
//.WithParameter("auth", authKey)
.WithParameter("channel", grantChannel)
.WithParameter("m", "1")
.WithParameter("pnsdk", PubnubCommon.EncodedSDK)
Expand All @@ -70,36 +70,37 @@ 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
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<string, PNTokenAuthValues>()
{
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)
{
grantManualEvent.Set();
}
}
grantChannel, fullAccess
}
}
catch { /* ignore */ }
finally { grantManualEvent.Set(); }
}));
var receivedGrantMessage = grantManualEvent.WaitOne(5000);
}).ExecuteAsync();

await Task.Delay(4000);

authToken = grantResult.Result?.Token;

pubnub.Destroy();
pubnub.PubnubUnitTest = null;
pubnub = null;
Assert.IsTrue(receivedGrantMessage, "WhenDetailedHistoryIsRequested Grant access failed.");
Assert.IsTrue(grantResult.Result != null && grantResult.Status.Error == false,
"WhenDetailedHistoryIsRequested Grant access failed.");
}

[TearDown]
Expand Down Expand Up @@ -132,14 +133,10 @@ public static void DetailHistoryNoStoreShouldNotGetMessage()
{
config.SecretKey = PubnubCommon.SecretKey;
}
else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey)
{
config.AuthKey = authKey;
}

server.RunOnHttps(false);

pubnub = createPubNubInstance(config);
pubnub = createPubNubInstance(config, authToken);

string channel = "hello_my_channel";
string message = messageForNoStorePublish;
Expand Down Expand Up @@ -240,18 +237,14 @@ public static void DetailHistoryShouldReturnDecryptMessage()
{
config.SecretKey = PubnubCommon.SecretKey;
}
else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey)
{
config.AuthKey = authKey;
}

server.RunOnHttps(false);
if (PubnubCommon.PAMServerSideRun)
{
config.AuthKey = "myAuth";
}

pubnub = createPubNubInstance(config);
pubnub = createPubNubInstance(config, authToken);

string channel = "hello_my_channel";
string message = messageForPublish;
Expand Down Expand Up @@ -353,18 +346,14 @@ public static void DetailHistoryCount10ReturnsRecords()
{
config.SecretKey = PubnubCommon.SecretKey;
}
else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey)
{
config.AuthKey = authKey;
}

server.RunOnHttps(false);
if (PubnubCommon.PAMServerSideRun)
{
config.AuthKey = "myAuth";
}

pubnub = createPubNubInstance(config);
pubnub = createPubNubInstance(config, authToken);

string channel = "hello_my_channel";
manualResetEventWaitTimeout = (PubnubCommon.EnableStubTest) ? 1000 : 310 * 1000;
Expand Down Expand Up @@ -417,18 +406,14 @@ public static void DetailHistoryCount10ReverseTrueReturnsRecords()
{
config.SecretKey = PubnubCommon.SecretKey;
}
else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey)
{
config.AuthKey = authKey;
}

server.RunOnHttps(false);
if (PubnubCommon.PAMServerSideRun)
{
config.AuthKey = "myAuth";
}

pubnub = createPubNubInstance(config);
pubnub = createPubNubInstance(config, authToken);

string channel = "hello_my_channel";

Expand Down Expand Up @@ -483,18 +468,14 @@ public static void DetailedHistoryStartWithReverseTrue()
{
config.SecretKey = PubnubCommon.SecretKey;
}
else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey)
{
config.AuthKey = authKey;
}

server.RunOnHttps(false);
if (PubnubCommon.PAMServerSideRun)
{
config.AuthKey = "myAuth";
}

pubnub = createPubNubInstance(config);
pubnub = createPubNubInstance(config, authToken);

string channel = "hello_my_channel";

Expand Down Expand Up @@ -735,14 +716,10 @@ private static void CommonDetailedHistoryShouldReturnEncryptedMessageBasedOnPara
{
config.SecretKey = secretKey;
}
else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey)
{
config.AuthKey = authKey;
}

server.RunOnHttps(ssl);

pubnub = createPubNubInstance(config);
pubnub = createPubNubInstance(config, authToken);

string channel = "hello_my_channel";

Expand Down Expand Up @@ -1076,14 +1053,10 @@ private static void CommonDetailedHistoryShouldReturnUnencryptedMessageBasedOnPa
{
config.SecretKey = secretKey;
}
else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey)
{
config.AuthKey = authKey;
}

server.RunOnHttps(ssl);

pubnub = createPubNubInstance(config);
pubnub = createPubNubInstance(config, authToken);

string channel = "hello_my_channel";

Expand Down
Loading

0 comments on commit ed038be

Please sign in to comment.