Skip to content

Commit

Permalink
Implement access level methods in UserApiClient. (#283)
Browse files Browse the repository at this point in the history
fix #282
  • Loading branch information
DiscoPYF committed Sep 30, 2020
1 parent 633f9aa commit a8ad515
Show file tree
Hide file tree
Showing 13 changed files with 796 additions and 22 deletions.
358 changes: 358 additions & 0 deletions arangodb-net-standard.Test/UserApi/UserApiClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using ArangoDBNetStandard;
using ArangoDBNetStandard.Transport;
using ArangoDBNetStandard.UserApi;
using ArangoDBNetStandard.UserApi.Models;
using Moq;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -192,5 +197,358 @@ public async Task GetUserAsync_ShouldThrow_WhenUserDoesNotExist()
Assert.NotNull(ex.ApiError.ErrorMessage);
Assert.Equal(1703, ex.ApiError.ErrorNum); // ERROR_USER_NOT_FOUND
}

[Fact]
public async Task PutDatabaseAccessLevelAsync_ShouldSucceed()
{
PutAccessLevelResponse response =
await _userClient.PutDatabaseAccessLevelAsync(
_fixture.UsernameExisting,
"_system",
new PutAccessLevelBody()
{
Grant = "rw"
});

Assert.False(response.Error);
Assert.Equal(HttpStatusCode.OK, response.Code);
}

[Fact]
public async Task PutDatabaseAccessLevelAsync_ShouldThrow_WhenUserDoesNotExist()
{
string username = nameof(PutDatabaseAccessLevelAsync_ShouldThrow_WhenUserDoesNotExist);

var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _userClient.PutDatabaseAccessLevelAsync(
username,
"_system",
new PutAccessLevelBody()
{
Grant = "rw"
});
});

Assert.True(ex.ApiError.Error);
Assert.Equal(HttpStatusCode.NotFound, ex.ApiError.Code);
Assert.NotNull(ex.ApiError.ErrorMessage);
Assert.Equal(1703, ex.ApiError.ErrorNum); // ERROR_USER_NOT_FOUND
}

[Fact]
public async Task GetDatabaseAccessLevelAsync_ShouldSucceed()
{
GetAccessLevelResponse response =
await _userClient.GetDatabaseAccessLevelAsync(
_fixture.UsernameExisting,
_fixture.TestDbName);

Assert.False(response.Error);
Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.False(string.IsNullOrEmpty(response.Result));
}

[Fact]
public async Task GetDatabaseAccessLevelAsync_ShouldThrow_WhenErrorResponseReturned()
{
// Arrange

var mockTransport = new Mock<IApiClientTransport>();

var mockResponse = new Mock<IApiClientResponse>();

var mockResponseContent = new Mock<IApiClientResponseContent>();

string mockJsonResponse = "{\"error\":true,\"errorMessage\":\"user not found\",\"errorNum\":1703,\"code\":404}";

mockResponseContent.Setup(x => x.ReadAsStreamAsync())
.Returns(Task.FromResult<Stream>(
new MemoryStream(Encoding.UTF8.GetBytes(mockJsonResponse))));

mockResponse.Setup(x => x.Content)
.Returns(mockResponseContent.Object);

mockResponse.Setup(x => x.IsSuccessStatusCode)
.Returns(false);

string requestUri = null;

mockTransport.Setup(x => x.GetAsync(It.IsAny<string>()))
.Returns((string uri) =>
{
requestUri = uri;
return Task.FromResult(mockResponse.Object);
});

var client = new UserApiClient(mockTransport.Object);

// Act

var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await client.GetDatabaseAccessLevelAsync("", "");
});

// Assert

Assert.True(ex.ApiError.Error);
Assert.Equal(HttpStatusCode.NotFound, ex.ApiError.Code);
Assert.NotNull(ex.ApiError.ErrorMessage);
Assert.Equal(1703, ex.ApiError.ErrorNum); // ERROR_USER_NOT_FOUND
}

[Fact]
public async Task DeleteDatabaseAccessLevelAsync_ShouldSucceed()
{
DeleteAccessLevelResponse response =
await _userClient.DeleteDatabaseAccessLevelAsync(
_fixture.UsernameToRemoveAccess,
_fixture.TestDbName);

Assert.False(response.Error);
Assert.Equal(HttpStatusCode.Accepted, response.Code);
}

[Fact]
public async Task DeleteDatabaseAccessLevelAsync_ShouldThrow_WhenUserDoesNotExist()
{
string username = nameof(DeleteDatabaseAccessLevelAsync_ShouldThrow_WhenUserDoesNotExist);

var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _userClient.DeleteDatabaseAccessLevelAsync(username, "_system");
});

Assert.True(ex.ApiError.Error);
Assert.Equal(HttpStatusCode.NotFound, ex.ApiError.Code);
Assert.NotNull(ex.ApiError.ErrorMessage);
Assert.Equal(1703, ex.ApiError.ErrorNum); // ERROR_USER_NOT_FOUND
}

[Fact]
public async Task GetAccessibleDatabasesAsync_ShouldSucceed()
{
GetAccessibleDatabasesResponse response =
await _userClient.GetAccessibleDatabasesAsync(_fixture.UsernameExisting);

Assert.False(response.Error);
Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.NotNull(response.Result);
Assert.True(response.Result.Keys.Count > 0);

string accessLevel = response.Result[response.Result.Keys.First()].ToString();
Assert.False(string.IsNullOrEmpty(accessLevel));
}

[Fact]
public async Task GetAccessibleDatabasesAsync_ShouldUseQueryParameters_WhenProvided()
{
var mockTransport = new Mock<IApiClientTransport>();

var mockResponse = new Mock<IApiClientResponse>();

var mockResponseContent = new Mock<IApiClientResponseContent>();

mockResponse.Setup(x => x.Content)
.Returns(mockResponseContent.Object);

mockResponse.Setup(x => x.IsSuccessStatusCode)
.Returns(true);

string requestUri = null;

mockTransport.Setup(x => x.GetAsync(It.IsAny<string>()))
.Returns((string uri) =>
{
requestUri = uri;
return Task.FromResult(mockResponse.Object);
});

var client = new UserApiClient(mockTransport.Object);

await client.GetAccessibleDatabasesAsync("", new GetAccessibleDatabasesQuery()
{
Full = true
});

Assert.NotNull(requestUri);
Assert.Contains("full=true", requestUri);
}

[Fact]
public async Task GetAccessibleDatabasesAsync_ShouldSucceed_WhenFullIsProvided()
{
GetAccessibleDatabasesResponse response =
await _userClient.GetAccessibleDatabasesAsync(
_fixture.UsernameExisting,
new GetAccessibleDatabasesQuery()
{
Full = true
});

Assert.False(response.Error);
Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.NotNull(response.Result);
Assert.True(response.Result.Keys.Count > 0);

object accessLevel = response.Result[response.Result.Keys.First()];
var jObject = accessLevel as Newtonsoft.Json.Linq.JObject;

Assert.NotNull(jObject);
Assert.True(jObject.ContainsKey("permission"));
Assert.True(jObject.ContainsKey("collections"));
}

[Fact]
public async Task GetAccessibleDatabasesAsync_ShouldThrow_WhenUserDoesNotExist()
{
string username = nameof(GetAccessibleDatabasesAsync_ShouldThrow_WhenUserDoesNotExist);

var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _userClient.GetAccessibleDatabasesAsync(username);
});

Assert.True(ex.ApiError.Error);
Assert.Equal(HttpStatusCode.NotFound, ex.ApiError.Code);
Assert.NotNull(ex.ApiError.ErrorMessage);
Assert.Equal(1703, ex.ApiError.ErrorNum); // ERROR_USER_NOT_FOUND
}

[Fact]
public async Task PutCollectionAccessLevelAsync_ShouldSucceed()
{
PutAccessLevelResponse response =
await _userClient.PutCollectionAccessLevelAsync(
_fixture.UsernameExisting,
_fixture.TestDbName,
_fixture.CollectionNameToSetAccess,
new PutAccessLevelBody()
{
Grant = "rw"
});

Assert.False(response.Error);
Assert.Equal(HttpStatusCode.OK, response.Code);
}

[Fact]
public async Task PutCollectionAccessLevelAsync_ShouldThrow_WhenUserDoesNotExist()
{
string username = nameof(PutCollectionAccessLevelAsync_ShouldThrow_WhenUserDoesNotExist);

var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _userClient.PutCollectionAccessLevelAsync(
username,
_fixture.TestDbName,
_fixture.CollectionNameToSetAccess,
new PutAccessLevelBody()
{
Grant = "rw"
});
});

Assert.True(ex.ApiError.Error);
Assert.Equal(HttpStatusCode.NotFound, ex.ApiError.Code);
Assert.NotNull(ex.ApiError.ErrorMessage);
Assert.Equal(1703, ex.ApiError.ErrorNum); // ERROR_USER_NOT_FOUND
}

[Fact]
public async Task GetCollectionAccessLevelAsync_ShouldSucceed()
{
GetAccessLevelResponse response =
await _userClient.GetCollectionAccessLevelAsync(
_fixture.UsernameExisting,
_fixture.TestDbName,
_fixture.CollectionNameToSetAccess);

Assert.False(response.Error);
Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.False(string.IsNullOrEmpty(response.Result));
}

[Fact]
public async Task GetCollectionAccessLevelAsync_ShouldThrow_WhenErrorResponseReturned()
{
// Arrange

var mockTransport = new Mock<IApiClientTransport>();

var mockResponse = new Mock<IApiClientResponse>();

var mockResponseContent = new Mock<IApiClientResponseContent>();

string mockJsonResponse = "{\"error\":true,\"errorMessage\":\"user not found\",\"errorNum\":1703,\"code\":404}";

mockResponseContent.Setup(x => x.ReadAsStreamAsync())
.Returns(Task.FromResult<Stream>(
new MemoryStream(Encoding.UTF8.GetBytes(mockJsonResponse))));

mockResponse.Setup(x => x.Content)
.Returns(mockResponseContent.Object);

mockResponse.Setup(x => x.IsSuccessStatusCode)
.Returns(false);

string requestUri = null;

mockTransport.Setup(x => x.GetAsync(It.IsAny<string>()))
.Returns((string uri) =>
{
requestUri = uri;
return Task.FromResult(mockResponse.Object);
});

var client = new UserApiClient(mockTransport.Object);

// Act

var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await client.GetCollectionAccessLevelAsync("", "", "");
});

// Assert

Assert.True(ex.ApiError.Error);
Assert.Equal(HttpStatusCode.NotFound, ex.ApiError.Code);
Assert.NotNull(ex.ApiError.ErrorMessage);
Assert.Equal(1703, ex.ApiError.ErrorNum); // ERROR_USER_NOT_FOUND
}

[Fact]
public async Task DeleteCollectionAccessLevelAsync_ShouldSucceed()
{
DeleteAccessLevelResponse response =
await _userClient.DeleteCollectionAccessLevelAsync(
_fixture.UsernameToRemoveAccess,
_fixture.TestDbName,
_fixture.CollectionNameToRemoveAccess);

Assert.False(response.Error);
Assert.Equal(HttpStatusCode.Accepted, response.Code);
}

[Fact]
public async Task DeleteCollectionAccessLevelAsync_ShouldThrow_WhenUserDoesNotExist()
{
string username = nameof(DeleteCollectionAccessLevelAsync_ShouldThrow_WhenUserDoesNotExist);

var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _userClient.DeleteCollectionAccessLevelAsync(
username,
_fixture.TestDbName,
_fixture.CollectionNameToRemoveAccess);
});

Assert.True(ex.ApiError.Error);
Assert.Equal(HttpStatusCode.NotFound, ex.ApiError.Code);
Assert.NotNull(ex.ApiError.ErrorMessage);
Assert.Equal(1703, ex.ApiError.ErrorNum); // ERROR_USER_NOT_FOUND
}
}
}

0 comments on commit a8ad515

Please sign in to comment.