From 4c0c1a2f4e7738b4337c6db3d816d9691434d1cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=81abno?= Date: Wed, 18 Dec 2024 12:34:37 +0000 Subject: [PATCH] Add Subusers feature #get --- smsapi/Api/Action/Subusers/GetSubuser.cs | 19 +++++ smsapi/Api/SubUsersFactory.cs | 9 +++ .../Action/Subusers/GetSubuserResponseTest.cs | 81 +++++++++++++++++++ .../Unit/Action/Subusers/GetSubuserTest.cs | 43 ++++++++++ 4 files changed, 152 insertions(+) create mode 100644 smsapi/Api/Action/Subusers/GetSubuser.cs create mode 100644 smsapiTests/Unit/Action/Subusers/GetSubuserResponseTest.cs create mode 100644 smsapiTests/Unit/Action/Subusers/GetSubuserTest.cs diff --git a/smsapi/Api/Action/Subusers/GetSubuser.cs b/smsapi/Api/Action/Subusers/GetSubuser.cs new file mode 100644 index 0000000..b195979 --- /dev/null +++ b/smsapi/Api/Action/Subusers/GetSubuser.cs @@ -0,0 +1,19 @@ +using SMSApi.Api.Response.Subusers; + +namespace SMSApi.Api.Action.Subusers; + +public class GetSubuser : Action +{ + 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; +} diff --git a/smsapi/Api/SubUsersFactory.cs b/smsapi/Api/SubUsersFactory.cs index cfdec60..1bd4c27 100644 --- a/smsapi/Api/SubUsersFactory.cs +++ b/smsapi/Api/SubUsersFactory.cs @@ -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 diff --git a/smsapiTests/Unit/Action/Subusers/GetSubuserResponseTest.cs b/smsapiTests/Unit/Action/Subusers/GetSubuserResponseTest.cs new file mode 100644 index 0000000..defb121 --- /dev/null +++ b/smsapiTests/Unit/Action/Subusers/GetSubuserResponseTest.cs @@ -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 + { + { "id", id }, + { "username", username }, + { "active", active }, + { "description", description }, + { + "points", new Dictionary + { + { "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(result); + } + + private SubuserDetails GetSubuser() + { + var action = new GetSubuser("any"); + action.Proxy(_proxyStub); + + return action.Execute(); + } +} diff --git a/smsapiTests/Unit/Action/Subusers/GetSubuserTest.cs b/smsapiTests/Unit/Action/Subusers/GetSubuserTest.cs new file mode 100644 index 0000000..31313f0 --- /dev/null +++ b/smsapiTests/Unit/Action/Subusers/GetSubuserTest.cs @@ -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; + } +}