Skip to content

Commit

Permalink
Adding new Secrets tests - adding HALLink to secret object
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 committed Apr 14, 2020
1 parent 5cc5d84 commit 0a1b5e5
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 6 deletions.
157 changes: 157 additions & 0 deletions Nexmo.Api.Test.Unit/AccountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,162 @@ public void GetNumbers()
Assert.Equal("mobile-lvn", numbers.Numbers[0].Type);
Assert.Equal("VOICE", numbers.Numbers[0].Features.First());
}

[Fact]
public void RetrieveApiSecretsWithKey()
{
RetrieveApiSecrets(ApiKey);
}

[Fact]
public void RetreiveApiSecretsWithNull()
{
RetrieveApiSecrets(null);
}

public void RetrieveApiSecrets(string apiKey)
{
//ARRANGE
var pathKey = apiKey != null ? apiKey : ApiKey;
var expectedResponse = @"{
""_links"": {
""self"": {
""href"": ""abc123""
}
},
""_embedded"": {
""secrets"": [
{
""_links"": {
""self"": {
""href"": ""abc123""
}
},
""id"": ""ad6dc56f-07b5-46e1-a527-85530e625800"",
""created_at"": ""2017-03-02T16:34:49Z""
}
]
}
}";
var expectedUri = $"https://api.nexmo.com/accounts/{pathKey}/secrets";
Setup(expectedUri, expectedResponse);

//ACT
var client = new NexmoClient(Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret));
var secrets = client.AccountClient.RetrieveApiSecrets(apiKey);

//ASSERT
Assert.Equal("ad6dc56f-07b5-46e1-a527-85530e625800", secrets.Embedded.Secrets[0].Id);
Assert.Equal("2017-03-02T16:34:49Z", secrets.Embedded.Secrets[0].CreatedAt);
Assert.Equal("abc123", secrets.Embedded.Secrets[0].Links.Self.Href);
Assert.Equal("abc123", secrets.Links.Self.Href);
}

[Fact]
public void CreateSecretWithKey()
{
CreateApiSecret(ApiKey);
}

[Fact]
public void CreateSecretWithNullKey()
{
CreateApiSecret(null);
}

public void CreateApiSecret(string apiKey)
{
//ARRANGE
var pathKey = apiKey != null ? apiKey : ApiKey;
var expectedUri = $"https://api.nexmo.com/accounts/{pathKey}/secrets";
var expectedResponse = @"{
""_links"": {
""self"": {
""href"": ""abc123""
}
},
""id"": ""ad6dc56f-07b5-46e1-a527-85530e625800"",
""created_at"": ""2017-03-02T16:34:49Z""
}";
Setup(expectedUri, expectedResponse);

//ACT
var client = new NexmoClient(Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret));
var secret = client.AccountClient.CreateApiSecret(new Accounts.CreateSecretRequest { Secret = "password" }, apiKey);

//ASSERT
Assert.Equal("ad6dc56f-07b5-46e1-a527-85530e625800", secret.Id);
Assert.Equal("2017-03-02T16:34:49Z", secret.CreatedAt);
Assert.Equal("abc123", secret.Links.Self.Href);
}

[Fact]
public void RetrieveSecretWithKey()
{
RetrieveSecret(ApiKey);
}

[Fact]
public void RetrieveSecretWithNull()
{
RetrieveSecret(null);
}

public void RetrieveSecret(string apiKey)
{

//ARRANGE
var pathKey = apiKey != null ? apiKey : ApiKey;
var secretId = "ad6dc56f-07b5-46e1-a527-85530e625800";
var expectedUri = $"https://api.nexmo.com/accounts/{pathKey}/secrets/{secretId}";
var expectedResponse = @"{
""_links"": {
""self"": {
""href"": ""abc123""
}
},
""id"": ""ad6dc56f-07b5-46e1-a527-85530e625800"",
""created_at"": ""2017-03-02T16:34:49Z""
}";
Setup(expectedUri, expectedResponse);

//ACT
var client = new NexmoClient(Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret));
var secret = client.AccountClient.RetrieveApiSecret(secretId, apiKey);

//ASSERT
Assert.Equal(secretId, secret.Id);
Assert.Equal("2017-03-02T16:34:49Z", secret.CreatedAt);
Assert.Equal("abc123", secret.Links.Self.Href);
}

[Fact]
public void RevokeWithKey()
{
RevokeSecret(ApiKey);
}

[Fact]
public void RevokeWithNull()
{
RevokeSecret(null);
}

public void RevokeSecret(string apiKey)
{
//ARRANGE
var pathKey = apiKey != null ? apiKey : ApiKey;
var secretId = "ad6dc56f-07b5-46e1-a527-85530e625800";
var expectedUri = $"https://api.nexmo.com/accounts/{pathKey}/secrets/{secretId}";
var expectedResponse = @"";
Setup(expectedUri, expectedResponse);

//ACT
var client = new NexmoClient(Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret));
var response = client.AccountClient.RevokeApiSecret(secretId, apiKey);

//ASSERT
Assert.True(response);
}
}
}
4 changes: 2 additions & 2 deletions Nexmo.Api.Test.Unit/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class TestBase
const string MOCKED_METHOD = "SendAsync";
protected string ApiUrl = Configuration.Instance.Settings["appSettings:Nexmo.Url.Api"];
protected string RestUrl = Configuration.Instance.Settings["appSettings:Nexmo.Url.Rest"];
protected string ApiKey = Configuration.Instance.Settings["appSettings:Nexmo.api_key"];
protected string ApiSecret = Configuration.Instance.Settings["appSettings:Nexmo.api_secret"];
protected string ApiKey = Environment.GetEnvironmentVariable("NEXMO_API_KEY")??"testKey";
protected string ApiSecret = Environment.GetEnvironmentVariable("NEXMO_API_Secret") ?? "testSecret";
public void Setup(string uri, string responseContent, string requestContent = null, HttpStatusCode expectedCode = HttpStatusCode.OK)
{
typeof(Configuration).GetField("_client", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(Configuration.Instance, null);
Expand Down
8 changes: 4 additions & 4 deletions Nexmo.Api/Accounts/AccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public AccountSettingsResult ChangeAccountSettings(AccountSettingsRequest reques

public SecretsRequestResult RetrieveApiSecrets(string apiKey = null, Credentials creds = null)
{
apiKey = apiKey ?? creds.ApiKey;
apiKey = apiKey ?? (creds?.ApiKey ?? Credentials.ApiKey);
apiKey = apiKey ?? Configuration.Instance.Settings["appSettings:Nexmo.api_key"];
return ApiRequest.DoGetRequestWithUrlContent<SecretsRequestResult>(
ApiRequest.GetBaseUri(ApiRequest.UriType.Api, $"/accounts/{apiKey}/secrets"),
Expand All @@ -53,7 +53,7 @@ public SecretsRequestResult RetrieveApiSecrets(string apiKey = null, Credentials

public Secret CreateApiSecret(CreateSecretRequest request, string apiKey = null, Credentials creds = null)
{
apiKey = apiKey ?? creds.ApiKey;
apiKey = apiKey ?? (creds?.ApiKey ?? Credentials.ApiKey);
apiKey = apiKey ?? Configuration.Instance.Settings["appSettings:Nexmo.api_key"];
return ApiRequest.DoRequestWithJsonContent<Secret>(
"POST",
Expand All @@ -66,7 +66,7 @@ public Secret CreateApiSecret(CreateSecretRequest request, string apiKey = null,

public Secret RetrieveApiSecret(string secretId, string apiKey = null, Credentials creds = null)
{
apiKey = apiKey ?? creds.ApiKey;
apiKey = apiKey ?? (creds?.ApiKey ?? Credentials.ApiKey);
apiKey = apiKey ?? Configuration.Instance.Settings["appSettings:Nexmo.api_key"];
return ApiRequest.DoGetRequestWithUrlContent<Secret>(
ApiRequest.GetBaseUri(ApiRequest.UriType.Api, $"/accounts/{apiKey}/secrets/{secretId}"),
Expand All @@ -77,7 +77,7 @@ public Secret RetrieveApiSecret(string secretId, string apiKey = null, Credentia

public bool RevokeApiSecret(string secretId, string apiKey = null, Credentials creds = null)
{
apiKey = apiKey ?? creds.ApiKey;
apiKey = apiKey ?? (creds?.ApiKey ?? Credentials.ApiKey);
apiKey = apiKey ?? Configuration.Instance.Settings["appSettings:Nexmo.api_key"];
ApiRequest.DoDeleteRequestWithUrlContent(
ApiRequest.GetBaseUri(ApiRequest.UriType.Api, $"/accounts/{apiKey}/secrets/{secretId}"),
Expand Down
4 changes: 4 additions & 0 deletions Nexmo.Api/Accounts/Secret.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using Newtonsoft.Json;
using Nexmo.Api.Common;

namespace Nexmo.Api.Accounts
{
public class Secret
{
[JsonProperty("_links")]
public HALLinks Links { get; set; }

[JsonProperty("id")]
public string Id { get; set; }

Expand Down

0 comments on commit 0a1b5e5

Please sign in to comment.