Skip to content

Commit

Permalink
Add Subusers feature #get
Browse files Browse the repository at this point in the history
  • Loading branch information
jakublabno committed Dec 18, 2024
1 parent 963297d commit 4c0c1a2
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
19 changes: 19 additions & 0 deletions smsapi/Api/Action/Subusers/GetSubuser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using SMSApi.Api.Response.Subusers;

namespace SMSApi.Api.Action.Subusers;

public class GetSubuser : Action<SubuserDetails>
{
private readonly string _userId;

public GetSubuser(string userId)
{
_userId = userId;
}

protected override RequestMethod Method => RequestMethod.GET;

protected override string Uri() => $"subusers/{_userId}";

protected override ApiType ApiType() => Action.ApiType.Rest;
}
9 changes: 9 additions & 0 deletions smsapi/Api/SubUsersFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public CreateSubuser Create(SubuserCredentials credentials)

return action;
}

public GetSubuser Get(string userId)
{
var action = new GetSubuser(userId);

action.Proxy(proxy);

return action;
}
}

public static class SubusersFeatureRegister
Expand Down
81 changes: 81 additions & 0 deletions smsapiTests/Unit/Action/Subusers/GetSubuserResponseTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Net;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SMSApi.Api;
using SMSApi.Api.Action.Subusers;
using smsapi.Api.Response.REST.Exception;
using SMSApi.Api.Response.Subusers;
using smsapiTests.Unit.Fixture;
using smsapiTests.Unit.Helper;

namespace smsapiTests.Unit.Action.Subusers;

[TestClass]
public class GetSubuserResponseTest
{
private readonly ProxyStub _proxyStub = new();

[TestMethod]
[DataRow(true)]
[DataRow(false)]
public void get_subuser(bool active)
{
var id = "655B26893332330011B0B297";
var username = "subuser_name";
var description = "any description";
var fromAccountPoints = Random.Shared.NextDouble();
var perMonthPoints = Random.Shared.NextDouble();
var response =
new Dictionary<string, dynamic>
{
{ "id", id },
{ "username", username },
{ "active", active },
{ "description", description },
{
"points", new Dictionary<string, double>
{
{ "from_account", fromAccountPoints },
{ "per_month", perMonthPoints }
}
}
};
_proxyStub.SyncExecutionResponse = new HttpResponseEntity(
response.ToHttpEntityStreamTask(),
HttpStatusCode.OK
);

var result = GetSubuser();

Assert.AreEqual(id, result.Id);
Assert.AreEqual(username, result.Username);
Assert.AreEqual(active, result.Active);
Assert.AreEqual(description, result.Description);
Assert.AreEqual(new UserPoints(fromAccountPoints, perMonthPoints), result.Points);
}

[TestMethod]
public void map_http_404_to_not_found_exception()
{
_proxyStub.SyncExecutionResponse = new HttpResponseEntity(
DictionaryToStreamHelper.EmptyStream,
HttpStatusCode.NotFound
);

var result = () =>
{
GetSubuser();
};

Assert.ThrowsException<NotFoundException>(result);
}

private SubuserDetails GetSubuser()
{
var action = new GetSubuser("any");
action.Proxy(_proxyStub);

return action.Execute();
}
}
43 changes: 43 additions & 0 deletions smsapiTests/Unit/Action/Subusers/GetSubuserTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SMSApi.Api;
using SMSApi.Api.Action.Subusers;

namespace smsapiTests.Unit.Action.Subusers;

[TestClass]
public class GetSubuserTest
{
private readonly ProxyAssert _proxyAssert;
private readonly SpyProxy _spyProxy = new();

public GetSubuserTest()
{
_proxyAssert = new ProxyAssert(_spyProxy);
}

[TestMethod]
public void use_get_request_method()
{
GetSubuser().Execute();

_proxyAssert.AssertRequestMethod(RequestMethod.GET);
}

[TestMethod]
public void request_proper_uri()
{
var userId = "1238f47da26ee45dc41fb987";

GetSubuser(userId).Execute();

_proxyAssert.AssertUriEquals($"subusers/{userId}");
}

private GetSubuser GetSubuser(string id = "any")
{
var action = new GetSubuser(id);
action.Proxy(_spyProxy);

return action;
}
}

0 comments on commit 4c0c1a2

Please sign in to comment.