Skip to content

Commit

Permalink
Add User class and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
artemlos committed Aug 29, 2024
1 parent d9132f0 commit 80cec03
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Company>Cryptolens AB</Company>
<Authors>Cryptolens AB</Authors>
<Version>4.0.47</Version>
<AssemblyVersion>4.0.47.1</AssemblyVersion>
<FileVersion>4.0.47.1</FileVersion>
<Version>4.0.48</Version>
<AssemblyVersion>4.0.48.1</AssemblyVersion>
<FileVersion>4.0.48.1</FileVersion>
<Description>An API documentation can be found at https://help.cryptolens.io/api/dotnet/.

This is a client API that serves as an interface to Cryptolens Web API (app.cryptolens.io/docs/api/).
Expand Down
6 changes: 3 additions & 3 deletions Cryptolens.Licensing/Cryptolens.Licensing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Company>Cryptolens AB</Company>
<Authors>Cryptolens AB</Authors>
<Version>4.0.47</Version>
<AssemblyVersion>4.0.47.1</AssemblyVersion>
<FileVersion>4.0.46.1</FileVersion>
<Version>4.0.48</Version>
<AssemblyVersion>4.0.48.1</AssemblyVersion>
<FileVersion>4.0.48.1</FileVersion>
<Description>An API documentation can be found at https://help.cryptolens.io/api/dotnet/.

This is a client API that serves as an interface to Cryptolens Web API (app.cryptolens.io/docs/api/).
Expand Down
66 changes: 66 additions & 0 deletions Cryptolens.Licensing/Models/WebAPIModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1365,5 +1365,71 @@ public class AuthDetails
/// https://app.cryptolens.io/docs/api/v3/Versioning
/// </summary>
public int Version { get; set; }
}
public class LoginUserModel : RequestModel
{
public string UserName { get; set; }
public string Password { get; set; }
}

public class LoginUserResult : BasicResult
{
public List<LicenseKey> LicenseKeys { get; set; }
}
public class RegisterUserModel : RequestModel
{
public string UserName { get; set; }
public string Password { get; set; }
public int CustomerId { get; set; }
public string CustomerSecret { get; set; }
public string Email { get; set; }
}
public class AssociateUserModel : RequestModel
{
public string UserName { get; set; }
public int CustomerId { get; set; }

}
public class DissociateModel : RequestModel
{
public string UserName { get; set; }
}

public class GetUsersModel : RequestModel
{
public string UserName { get; set; }
public string Email { get; set; }
public int CustomerId { get; set; }
}
public class GetUsersResult : BasicResult
{
public List<UserObj> Users { get; set; }
}
public class UserObj
{
public string Username { get; set; }
public string Email { get; set; }
public DateTime Created { get; set; }
public int CustomerId { get; set; }
}
public class ChangePasswordModel : RequestModel
{
public string UserName { get; set; }
public string OldPassword { get; set; }
public string NewPassword { get; set; }
public string PasswordResetToken { get; set; }
public bool AdminMode { get; set; }
}
public class ResetPasswordModel : RequestModel
{
public string UserName { get; set; }
}
public class ResetPasswordResult : BasicResult
{
public string PasswordResetToken { get; set; }
}
public class RemoveUserModel : RequestModel
{
public string UserName { get; set; }
}
}
101 changes: 101 additions & 0 deletions Cryptolens.Licensing/SKMv3/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using SKM.V3.Models;
using SKM.V3.Internal;

namespace SKM.V3.Methods
{
/// <summary>
/// Methods that perform operations on a user object. A complete list can be found here: https://app.cryptolens.io/docs/api/v3/UserAuth
/// </summary>
public class UserAuth
{
/// <summary>
/// This method will return all licenses that belong to the user.
/// </summary>
/// <param name="token">This method can be called with an access token that has UserAuthNormal and UserAuthAdmin permission. More info: https://app.cryptolens.io/docs/api/v3/Auth </param>
/// <param name="parameters">Parameters of the method.</param>
/// <returns></returns>
public static LoginUserResult Login(string token, LoginUserModel parameters)
{
return HelperMethods.SendRequestToWebAPI3<LoginUserResult>(parameters, "/userauth/login/", token);
}

/// <summary>
/// This method will register a new user.
/// </summary>
/// <param name="token">This method can be called with an access token that has UserAuthAdmin permission. More info: https://app.cryptolens.io/docs/api/v3/Auth </param>
/// <param name="parameters">Parameters of the method.</param>
/// <returns></returns>
public static BasicResult Register(string token, RegisterUserModel parameters)
{
return HelperMethods.SendRequestToWebAPI3<BasicResult>(parameters, "/userauth/register/", token);
}


/// <summary>
/// Associates a user with a customer object.
/// </summary>
/// <param name="token">This method can be called with an access token that has UserAuthAdmin permission. More info: https://app.cryptolens.io/docs/api/v3/Auth </param>
/// <param name="parameters">Parameters of the method.</param>
/// <returns></returns>
public static BasicResult Associate(string token, AssociateUserModel parameters)
{
return HelperMethods.SendRequestToWebAPI3<BasicResult>(parameters, "/userauth/Associate/", token);
}

/// <summary>
/// Dissociates a user from a customer customer object.
/// </summary>
/// <param name="token">This method can be called with an access token that has UserAuthAdmin permission. More info: https://app.cryptolens.io/docs/api/v3/Auth </param>
/// <param name="parameters">Parameters of the method.</param>
/// <returns></returns>
public static BasicResult Dissociate(string token, DissociateModel parameters)
{
return HelperMethods.SendRequestToWebAPI3<BasicResult>(parameters, "/userauth/Dissociate/", token);
}


/// <summary>
/// List all registered users.
/// </summary>
/// <param name="token">This method can be called with an access token that has UserAuthAdmin permission. More info: https://app.cryptolens.io/docs/api/v3/Auth </param>
/// <param name="parameters">Parameters of the method.</param>
/// <returns></returns>
public static GetUsersResult GetUsers(string token, GetUsersModel parameters)
{
return HelperMethods.SendRequestToWebAPI3<GetUsersResult>(parameters, "/userauth/GetUsers/", token);
}

/// <summary>
/// This method will change the password of a user. It supports 3 modes of operation. With an access token that has UserAuthNormal permission (i.e. without admin permission), the password can either be changed by providing the old password or a password reset token, which can be generated using Reset Password Token method. Finally, if you call this method with an access token that has UserAuthAdmin permission, it will allow you to set AdminMode to True and only provide the NewPassword.
/// </summary>
/// <param name="token">This method can be called with an access token that has UserAuthNormal or UserAuthAdmin permission. More info: https://app.cryptolens.io/docs/api/v3/Auth </param>
/// <param name="parameters">Parameters of the method.</param>
/// <returns></returns>
public static BasicResult ChangePassword(string token, ChangePasswordModel parameters)
{
return HelperMethods.SendRequestToWebAPI3<BasicResult>(parameters, "/userauth/ChangePassword/", token);
}

/// <summary>
/// This method allows you to retrive the password reset token that you can use when calling Change Password method.
/// </summary>
/// <param name="token">This method can be called with an access token that has UserAuthAdmin permission. More info: https://app.cryptolens.io/docs/api/v3/Auth </param>
/// <param name="parameters">Parameters of the method.</param>
/// <returns></returns>
public static ResetPasswordResult ResetPasswordToken(string token, ResetPasswordModel parameters)
{
return HelperMethods.SendRequestToWebAPI3<ResetPasswordResult>(parameters, "/userauth/ResetPasswordToken/", token);
}

/// <summary>
/// This method removes a user.
/// </summary>
/// <param name="token">This method can be called with an access token that has UserAuthAdmin permission. More info: https://app.cryptolens.io/docs/api/v3/Auth </param>
/// <param name="parameters">Parameters of the method.</param>
/// <returns></returns>
public static BasicResult RemoveUser(string token, RemoveUserModel parameters)
{
return HelperMethods.SendRequestToWebAPI3<BasicResult>(parameters, "/userauth/RemoveUser/", token);
}
}
}
1 change: 1 addition & 0 deletions SKM Test/SKM Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
<Compile Include="TestSaveAndLoadFile.cs" />
<Compile Include="TestHelperMethods.cs" />
<Compile Include="TestHelpers.cs" />
<Compile Include="V3\TestUser.cs" />
<Compile Include="V3\TestDataObject.cs" />
<Compile Include="TestMachineCode.cs" />
<Compile Include="TestSKMTimeMethods.cs" />
Expand Down
77 changes: 77 additions & 0 deletions SKM Test/V3/TestUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SKM.V3.Methods;
using SKM.V3.Models;

namespace SKM_Test
{
[TestClass]
public class TestUser
{
[TestMethod]
public void LoginTest()
{
//var result = CustomerMethods.AddCustomer(AccessToken.AccessToken.CustomerAddRemoveAccessToken,
// new AddCustomerModel() { Name = "Bob" });

var result1 = UserAuth.Register(AccessToken.AccessToken.UserAuthAdmin, new RegisterUserModel { UserName = "test1", Password = "verysecurepassword" });

if (!Helpers.IsSuccessful(result1))
{
// error
Assert.Fail();
}

var result = UserAuth.Login(AccessToken.AccessToken.UserAuthAdmin, new LoginUserModel { UserName = "test1", Password = "verysecurepassword" });


if(!Helpers.IsSuccessful(result))
{
// error
Assert.Fail();

}

var passchange = UserAuth.ChangePassword(AccessToken.AccessToken.UserAuthAdmin, new ChangePasswordModel { UserName = "test1", OldPassword = "verysecurepassword", NewPassword ="test" });


if (!Helpers.IsSuccessful(passchange))
{
// error
Assert.Fail();

}


var log = UserAuth.Login(AccessToken.AccessToken.UserAuthAdmin, new LoginUserModel { UserName = "test1", Password="test" });


if (!Helpers.IsSuccessful(log))
{
// error
Assert.Fail();

}




var remove = UserAuth.RemoveUser(AccessToken.AccessToken.UserAuthAdmin, new RemoveUserModel { UserName = "test1"});


if (!Helpers.IsSuccessful(result))
{
// error
Assert.Fail();

}



}

}
}

0 comments on commit 80cec03

Please sign in to comment.