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

fix: Adding TaskRouter Grant #743

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
855a120
fix: adding TaskRouter Grant
tiwarishubham635 May 9, 2024
ccfef99
docs: corrected param name
tiwarishubham635 May 9, 2024
3df6eb4
chore: added test coverage for taskRouter object
tiwarishubham635 May 9, 2024
57da102
chore: replacing sonar.login with sonar.token
tiwarishubham635 May 13, 2024
8f7d67e
chore: adding coverlet package
tiwarishubham635 May 14, 2024
5d9f5a2
chore: adding msbuild in Test file
tiwarishubham635 May 16, 2024
146288d
chore: corrected reportsPath in sonar
tiwarishubham635 May 21, 2024
ff14df6
chore: corrected reportsPath to net462
tiwarishubham635 May 21, 2024
a28edcc
chore: correcting net462
tiwarishubham635 May 21, 2024
147875e
Update Makefile
tiwarishubham635 May 21, 2024
05ff34c
chore: added net462 in test.csproj
tiwarishubham635 May 21, 2024
d97deaa
chore: removed nullable
tiwarishubham635 May 21, 2024
e564ea9
chore: removing net6.0 from test
tiwarishubham635 May 21, 2024
c2fb085
chore: removing net462 from makefile
tiwarishubham635 May 21, 2024
bf4cc99
chore: added net6.0
tiwarishubham635 May 21, 2024
2fc6fe3
Update Makefile
tiwarishubham635 May 21, 2024
a01772c
chore: adding a test
tiwarishubham635 May 23, 2024
3dc6e1c
chore: removing net462
tiwarishubham635 May 23, 2024
9faa5c6
Merge branch 'main' into add_taskrouter_grant
tiwarishubham635 May 23, 2024
277c821
chore: added versbose
tiwarishubham635 May 27, 2024
c3f167d
Merge branch 'main' into add_taskrouter_grant
tiwarishubham635 May 27, 2024
83cb7bf
chore: adding sonarqubetestproject
tiwarishubham635 May 27, 2024
7f09237
Update Twilio.csproj
tiwarishubham635 May 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ docker-push:
docker push twilio/twilio-csharp:${CURRENT_TAG}

cover:
dotnet sonarscanner begin /k:"$(PROJECT_NAME)" /o:"twilio" /d:sonar.host.url=https://sonarcloud.io /d:sonar.login="${SONAR_TOKEN}" /d:sonar.language="cs" $(SONAR_SOURCES) /d:sonar.cs.opencover.reportsPaths="test/lcov.net451.opencover.xml"
dotnet sonarscanner begin /k:"$(PROJECT_NAME)" /o:"twilio" /d:sonar.host.url=https://sonarcloud.io /d:sonar.token="${SONAR_TOKEN}" /d:sonar.language="cs" $(SONAR_SOURCES) /d:sonar.cs.opencover.reportsPaths="test/lcov.opencover.xml" /d:"sonar.verbose=true"
# Write to a log file since the logs for build with sonar analyzer are pretty beefy and travis has a limit on the number of log lines
dotnet build Twilio.sln > buildsonar.log
dotnet test test/Twilio.Test/Twilio.Test.csproj --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=../lcov
dotnet sonarscanner end /d:sonar.login="${SONAR_TOKEN}"
dotnet sonarscanner end /d:sonar.token="${SONAR_TOKEN}"

cache:
directories:
Expand Down
67 changes: 67 additions & 0 deletions src/Twilio/JWT/AccessToken/TaskRouterGrant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Collections.Generic;

namespace Twilio.Jwt.AccessToken
{
/// <summary>
/// Grant to use for Twilio TaskRouter
/// </summary>
public class TaskRouterGrant : IGrant
{
/// <summary>
/// Workspace SID
/// </summary>
public string WorkspaceSid { get; set; }

/// <summary>
/// Worker SID
/// </summary>
public string WorkerSid { get; set; }

/// <summary>
/// Role
/// </summary>
public string Role { get; set; }

/// <summary>
/// Get the grant name
/// </summary>
///
/// <returns>name of the grant</returns>
public string Key
{
get
{
return "task_router";
}
}

/// <summary>
/// Get the grant payload
/// </summary>
///
/// <returns>payload of the grant</returns>
public object Payload
{
get
{
var payload = new Dictionary<string, string>();

if (WorkspaceSid != null)
{
payload.Add("workspace_sid", WorkspaceSid);
}
if (WorkerSid != null)
{
payload.Add("worker_sid", WorkerSid);
}
if (Role != null)
{
payload.Add("role", Role);
}

return payload;
}
}

}
}
1 change: 1 addition & 0 deletions src/Twilio/Twilio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard2.1' ">2.1.0</NetStandardImplicitPackageVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<SonarQubeTestProject>false</SonarQubeTestProject>
</PropertyGroup>
<ItemGroup>
<None Include="../../README.md" Pack="true" PackagePath="\"/>
Expand Down
62 changes: 62 additions & 0 deletions test/Twilio.Test/Jwt/AccessToken/AccessTokenTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,68 @@ public void TestCreateVoiceGrant()
Assert.AreEqual("bar", ToDict(decodedParams)["foo"]);
}

[Test]
public void TestTaskRouterProperties()
{
var taskRouterGrant = new TaskRouterGrant();
taskRouterGrant.WorkspaceSid = "WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
Assert.AreEqual("WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", taskRouterGrant.WorkspaceSid);
}

[Test]
public void TestCheckTaskRouter()
{
var taskRouterGrant = new TaskRouterGrant
{
WorkspaceSid = "WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
WorkerSid = "WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
Role = "worker"
};

Assert.IsNotNull(taskRouterGrant);
Assert.AreEqual("task_router", taskRouterGrant.Key);
var taskRouterPayload = (Dictionary<string, string>)(taskRouterGrant.Payload);
Assert.AreEqual(3, taskRouterPayload.Count);
Assert.AreEqual("WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", taskRouterPayload["workspace_sid"]);
Assert.AreEqual("WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", taskRouterPayload["worker_sid"]);
Assert.AreEqual("worker", taskRouterPayload["role"]);
}

[Test]
public void TestCreateTaskRouterGrant()
{
var grants = new HashSet<IGrant>
{
{
new TaskRouterGrant
{
WorkspaceSid = "WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
WorkerSid = "WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
Role = "worker"
}
}
};
var token = new TestToken("AC456", "SK123", Secret, grants: grants).ToJwt();
Assert.IsNotNull(token);
Assert.IsNotEmpty(token);

var decoded = new DecodedJwt(token, Secret);
var payload = decoded.Payload;
Assert.IsNotNull(payload);

Assert.AreEqual("SK123", payload["iss"]);
Assert.AreEqual("AC456", payload["sub"]);
Assert.Greater(Convert.ToInt64(payload["exp"]), BaseJwt.ConvertToUnixTimestamp(DateTime.UtcNow));

var decodedGrants = ToDict(payload["grants"]);
Assert.AreEqual(1, decodedGrants.Count);

var decodedCg = ToDict(decodedGrants["task_router"]);
Assert.AreEqual("WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", decodedCg["workspace_sid"]);
Assert.AreEqual("WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", decodedCg["worker_sid"]);
Assert.AreEqual("worker", decodedCg["role"]);
}

[Test]
public void TestCreateChatGrant()
{
Expand Down
6 changes: 6 additions & 0 deletions test/Twilio.Test/Twilio.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
<NoWarn>$(NoWarn);CS1591</NoWarn>
<NoWarn>$(NoWarn);CS1587</NoWarn>
<NoWarn>CS8765CS1587;CS1591;CS1570;CS1573;CS1572;CS8618;CS8603;CS8625;CS8604;CS8765</NoWarn>
<SonarQubeTestProject>true</SonarQubeTestProject>

</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.TestPlatform" Version="17.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
<PackageReference Include="NUnitLite" Version="3.13.2" />

<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Loading