Skip to content

Commit

Permalink
Add Subusers feature #edit
Browse files Browse the repository at this point in the history
  • Loading branch information
jakublabno committed Dec 19, 2024
1 parent 388052d commit fcebc52
Show file tree
Hide file tree
Showing 3 changed files with 331 additions and 0 deletions.
98 changes: 98 additions & 0 deletions smsapi/Api/Action/Subusers/Creation/EditSubuser.cs
Original file line number Diff line number Diff line change
@@ -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<SubuserDetails>
{
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<KeyValuePair<string, dynamic?>>?) Values()
{
var values = new HashSet<KeyValuePair<string, dynamic?>>();

_active?.Let(newStatus => values.Add(("active", newStatus)));

_password?.Let(newPassword =>
{
values.Add(
("credentials", new Dictionary<string, string> { { "password", newPassword } })
);
});

_desription?.Let(newDescription => values.Add(("description", newDescription)));

_points?.Let(points =>
{
var pointsStructure = new Dictionary<string, double>();

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);
}
}
64 changes: 64 additions & 0 deletions smsapiTests/Unit/Action/Subusers/EditSubuserResponseTest.cs
Original file line number Diff line number Diff line change
@@ -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<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.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();
}
}
169 changes: 169 additions & 0 deletions smsapiTests/Unit/Action/Subusers/EditSubuserTest.cs
Original file line number Diff line number Diff line change
@@ -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<string, string> { { "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<string, double> { { "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<string, double> { { "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<string, double>
{
{ "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;
}
}

0 comments on commit fcebc52

Please sign in to comment.