Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor: Test execution optimisation and workflow update for triggering tests. #236

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
8 changes: 7 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
name: Tests

on:
push:
pull_request:
branches:
- master
types:
- ready_for_review
- synchronize
- reopened
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
2 changes: 2 additions & 0 deletions src/UnitTests/PubnubApi.Tests/PubnubCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
89 changes: 89 additions & 0 deletions src/UnitTests/PubnubApi.Tests/TestHarness.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using PubnubApi;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;

namespace PubNubMessaging.Tests
{
Expand Down Expand Up @@ -37,5 +40,91 @@ 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";
string channelPattern = "foo.*";

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<string, PNTokenAuthValues>()
{
{
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<string, PNTokenAuthValues>()
{
{group, fullAccess},
{group+"-pnpres", fullAccess}
}
})
.Patterns(new PNTokenPatterns()
{
Channels = new Dictionary<string, PNTokenAuthValues>()
{
{ channelPattern, fullAccess },
{ channelPattern+"-pnpres", fullAccess }
}
})
.ExecuteAsync();

await Task.Delay(4000);

PubnubCommon.GrantToken = grantResult.Result?.Token;
Assert.IsTrue(grantResult.Status.Error == false && grantResult.Result != null,
"GrantToken() failed.");
}
}
}
67 changes: 6 additions & 61 deletions src/UnitTests/PubnubApi.Tests/WhenAClientIsPresented.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,71 +71,16 @@ 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<string, PNTokenAuthValues>()
{
{
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);
}
authToken = PubnubCommon.GrantToken;

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

[TearDown]
Expand Down
Loading
Loading