diff --git a/smsapi/Api/Action/Subusers/Creation/EditSubuser.cs b/smsapi/Api/Action/Subusers/Creation/EditSubuser.cs new file mode 100644 index 0000000..7e8cbd3 --- /dev/null +++ b/smsapi/Api/Action/Subusers/Creation/EditSubuser.cs @@ -0,0 +1,98 @@ +using System.Collections.Generic; +using System.Collections.Specialized; +using SMSApi.Api.Response.Subusers; + +namespace SMSApi.Api.Action.Subusers.Creation; + +public sealed class EditSubuser : Action +{ + private readonly string _userId; + + private bool? _active; + private string? _desription; + private string? _password; + private SubuserPoints? _points; + + public EditSubuser(string userId) + { + _userId = userId; + } + + protected override RequestMethod Method => RequestMethod.PUT; + + protected override ActionContentType ContentType => ActionContentType.Json; + + protected override ApiType ApiType() + { + return Action.ApiType.Rest; + } + + protected override string Uri() + { + return $"subusers/{_userId}"; + } + + public EditSubuser Activate() + { + _active = true; + + return this; + } + + public EditSubuser Deactivate() + { + _active = false; + + return this; + } + + public EditSubuser ChangeDescription(string description) + { + _desription = description; + + return this; + } + + public EditSubuser ChangePoints(SubuserPoints points) + { + _points = points; + + return this; + } + + public EditSubuser ChangePassword(string newPassword) + { + _password = newPassword; + + return this; + } + + protected override (NameValueCollection, ISet>?) Values() + { + var values = new HashSet>(); + + _active?.Let(newStatus => values.Add(("active", newStatus))); + + _password?.Let(newPassword => + { + values.Add( + ("credentials", new Dictionary { { "password", newPassword } }) + ); + }); + + _desription?.Let(newDescription => values.Add(("description", newDescription))); + + _points?.Let(points => + { + var pointsStructure = new Dictionary(); + + points.FromAccount?.Let(fromAccount => pointsStructure.Add("from_account", fromAccount)); + points.PerMonth?.Let(perMonth => pointsStructure.Add("per_month", perMonth)); + + if (pointsStructure.Count > 0) + values.Add(("points", pointsStructure)); + }); + + return (new NameValueCollection(), values); + } +} diff --git a/smsapiTests/Unit/Action/Subusers/EditSubuserResponseTest.cs b/smsapiTests/Unit/Action/Subusers/EditSubuserResponseTest.cs new file mode 100644 index 0000000..2a24e41 --- /dev/null +++ b/smsapiTests/Unit/Action/Subusers/EditSubuserResponseTest.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Net; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SMSApi.Api; +using SMSApi.Api.Action.Subusers.Creation; +using SMSApi.Api.Response.Subusers; +using smsapiTests.Unit.Fixture; +using smsapiTests.Unit.Helper; + +namespace smsapiTests.Unit.Action.Subusers; + +[TestClass] +public class EditSubuserResponseTest +{ + private readonly ProxyStub _proxyStub = new(); + + [TestMethod] + [DataRow(true)] + [DataRow(false)] + public void edit_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.Created + ); + + var result = EditSubuser(); + + 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); + } + + private SubuserDetails EditSubuser() + { + var action = new EditSubuser("any"); + action.Proxy(_proxyStub); + + return action.Execute(); + } +} diff --git a/smsapiTests/Unit/Action/Subusers/EditSubuserTest.cs b/smsapiTests/Unit/Action/Subusers/EditSubuserTest.cs new file mode 100644 index 0000000..8888196 --- /dev/null +++ b/smsapiTests/Unit/Action/Subusers/EditSubuserTest.cs @@ -0,0 +1,169 @@ +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SMSApi.Api; +using SMSApi.Api.Action.Subusers.Creation; + +namespace smsapiTests.Unit.Action.Subusers; + +[TestClass] +public class EditSubuserTest +{ + private readonly ProxyAssert _proxyAssert; + private readonly SpyProxy _spyProxy = new(); + + public EditSubuserTest() + { + _proxyAssert = new ProxyAssert(_spyProxy); + } + + [TestMethod] + public void use_put_request_method() + { + EditSubuser().Execute(); + + _proxyAssert.AssertRequestMethod(RequestMethod.PUT); + } + + [TestMethod] + public void request_proper_uri() + { + var userId = "1238f47da26ee45dc41fb987"; + + EditSubuser(userId).Execute(); + + _proxyAssert.AssertUriEquals($"subusers/{userId}"); + } + + [TestMethod] + public void do_not_change_anything_when_not_requested() + { + EditSubuser().Execute(); + + _proxyAssert.AssertNoParameters(); + } + + [TestMethod] + public void activate_user() + { + EditSubuser() + .Activate() + .Execute(); + + _proxyAssert + .AssertParametersCount(1) + .AssertParametersContain("active", true); + } + + [TestMethod] + public void deactivate_user() + { + EditSubuser() + .Deactivate() + .Execute(); + + _proxyAssert + .AssertParametersCount(1) + .AssertParametersContain("active", false); + } + + [TestMethod] + public void change_password() + { + var newPassword = "newPassword"; + + EditSubuser() + .ChangePassword(newPassword) + .Execute(); + + _proxyAssert + .AssertParametersCount(1) + .AssertParametersContain("credentials", new Dictionary { { "password", newPassword } }); + } + + [TestMethod] + public void change_description() + { + var newDescription = "any description"; + + EditSubuser() + .ChangeDescription(newDescription) + .Execute(); + + _proxyAssert + .AssertParametersCount(1) + .AssertParametersContain("description", newDescription); + } + + [TestMethod] + public void do_not_change_points_when_empty() + { + var emptyPoints = new SubuserPoints(); + + EditSubuser() + .ChangePoints(emptyPoints) + .Execute(); + + _proxyAssert.AssertNoParameters(); + } + + [TestMethod] + public void send_only_from_account_points_value() + { + var fromAccount = 10; + var points = new SubuserPoints(fromAccount); + + EditSubuser() + .ChangePoints(points) + .Execute(); + + var expectedPoints = new Dictionary { { "from_account", fromAccount } }; + _proxyAssert + .AssertParametersCount(1) + .AssertParametersContain("points", expectedPoints); + } + + [TestMethod] + public void send_only_per_month_points_value() + { + var perMonth = 10; + var points = new SubuserPoints(PerMonth: perMonth); + + EditSubuser() + .ChangePoints(points) + .Execute(); + + var expectedPoints = new Dictionary { { "per_month", perMonth } }; + _proxyAssert + .AssertParametersCount(1) + .AssertParametersContain("points", expectedPoints); + } + + [TestMethod] + public void send_from_account_and_per_month_points_value() + { + var fromAccount = 15; + var perMonth = 10; + var points = new SubuserPoints(fromAccount, perMonth); + + EditSubuser() + .ChangePoints(points) + .Execute(); + + var expectedPoints = new Dictionary + { + { "from_account", fromAccount }, + { "per_month", perMonth } + }; + _proxyAssert + .AssertParametersCount(1) + .AssertParametersContain("points", expectedPoints); + } + + private EditSubuser EditSubuser(string userId = "any") + { + var action = new EditSubuser(userId); + action.Proxy(_spyProxy); + + return action; + } +}