-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from PinguApps/85-feat-account-list-logs
Implemented list logs
- Loading branch information
Showing
13 changed files
with
769 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PinguApps.Appwrite.Shared.Responses; | ||
|
||
/// <summary> | ||
/// Ann Appwrite Log object | ||
/// </summary> | ||
/// <param name="Event">Event name</param> | ||
/// <param name="UserId">User ID</param> | ||
/// <param name="UserEmail">User Email</param> | ||
/// <param name="UserName">User Name</param> | ||
/// <param name="Mode">API mode when event triggered</param> | ||
/// <param name="Ip">IP session in use when the session was created</param> | ||
/// <param name="Time">Log creation date in ISO 8601 format</param> | ||
/// <param name="OsCode">Operating system code name. View list of <see href="https://github.com/appwrite/appwrite/blob/master/docs/lists/os.json">Available Options</see></param> | ||
/// <param name="OsName">Operating system name</param> | ||
/// <param name="OsVersion">Operating system version</param> | ||
/// <param name="ClientType">Client type</param> | ||
/// <param name="ClientCode">Client code name. View list of <see href="https://github.com/appwrite/appwrite/blob/master/docs/lists/clients.json">Available Options</see></param> | ||
/// <param name="ClientName">Client name</param> | ||
/// <param name="ClientVersion">Client version</param> | ||
/// <param name="ClientEngine">Client engine name</param> | ||
/// <param name="ClientEngineVersion">Client engine version</param> | ||
/// <param name="DeviceName">Device name</param> | ||
/// <param name="DeviceBrand">Device brand name</param> | ||
/// <param name="DeviceModel">Device model name</param> | ||
/// <param name="CountryCode">Country two-character ISO 3166-1 alpha code</param> | ||
/// <param name="CountryName">Country name</param> | ||
public record LogModel( | ||
[property: JsonPropertyName("event")] string Event, | ||
[property: JsonPropertyName("userId")] string UserId, | ||
[property: JsonPropertyName("userEmail")] string UserEmail, | ||
[property: JsonPropertyName("userName")] string UserName, | ||
[property: JsonPropertyName("mode")] string Mode, | ||
[property: JsonPropertyName("ip")] string Ip, | ||
[property: JsonPropertyName("time")] DateTime Time, | ||
[property: JsonPropertyName("osCode")] string OsCode, | ||
[property: JsonPropertyName("osName")] string OsName, | ||
[property: JsonPropertyName("osVersion")] string OsVersion, | ||
[property: JsonPropertyName("clientType")] string ClientType, | ||
[property: JsonPropertyName("clientCode")] string ClientCode, | ||
[property: JsonPropertyName("clientName")] string ClientName, | ||
[property: JsonPropertyName("clientVersion")] string ClientVersion, | ||
[property: JsonPropertyName("clientEngine")] string ClientEngine, | ||
[property: JsonPropertyName("clientEngineVersion")] string ClientEngineVersion, | ||
[property: JsonPropertyName("deviceName")] string DeviceName, | ||
[property: JsonPropertyName("deviceBrand")] string DeviceBrand, | ||
[property: JsonPropertyName("deviceModel")] string DeviceModel, | ||
[property: JsonPropertyName("countryCode")] string CountryCode, | ||
[property: JsonPropertyName("countryName")] string CountryName | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PinguApps.Appwrite.Shared.Responses; | ||
|
||
/// <summary> | ||
/// An Appwrite Logs List object | ||
/// </summary> | ||
/// <param name="Total">Total number of logs documents that matched your query.</param> | ||
/// <param name="Logs">List of logs. Can be one of: <see cref="LogModel"/></param> | ||
public record LogsList( | ||
[property: JsonPropertyName("total")] int Total, | ||
[property: JsonPropertyName("logs")] List<LogModel> Logs | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PinguApps.Appwrite.Shared.Utils; | ||
public class Query | ||
{ | ||
[JsonPropertyName("method")] | ||
public string Method { get; private set; } | ||
|
||
[JsonPropertyName("attribute")] | ||
public string? Attribute { get; private set; } | ||
|
||
[JsonPropertyName("values")] | ||
public List<object>? Values { get; private set; } | ||
|
||
private Query(string method, string? attribute, object? values) | ||
{ | ||
Method = method; | ||
Attribute = attribute; | ||
|
||
if (values is IEnumerable<object> objects) | ||
{ | ||
Values = objects.ToList(); | ||
} | ||
else if (values is ICollection valuesList) | ||
{ | ||
Values = [.. valuesList]; | ||
} | ||
else if (values is not null) | ||
{ | ||
Values = [values]; | ||
} | ||
} | ||
|
||
public string GetQueryString() => Uri.EscapeUriString(JsonSerializer.Serialize(this)); | ||
|
||
public static Query Equal(string attribute, object value) => new("equal", attribute, value); | ||
|
||
public static Query NotEqual(string attribute, object value) => new("notEqual", attribute, value); | ||
|
||
public static Query LessThan(string attribute, object value) => new("lessThan", attribute, value); | ||
|
||
public static Query LessThanEqual(string attribute, object value) => new("lessThanEqual", attribute, value); | ||
|
||
public static Query GreaterThan(string attribute, object value) => new("greaterThan", attribute, value); | ||
|
||
public static Query GreaterThanEqual(string attribute, object value) => new("greaterThanEqual", attribute, value); | ||
|
||
public static Query Search(string attribute, object value) => new("search", attribute, value); | ||
|
||
public static Query IsNull(string attribute) => new("isNull", attribute, null); | ||
|
||
public static Query IsNotNull(string attribute) => new("isNotNull", attribute, null); | ||
|
||
public static Query StartsWith(string attribute, object value) => new("startsWith", attribute, value); | ||
|
||
public static Query EndsWith(string attribute, object value) => new("endsWith", attribute, value); | ||
|
||
public static Query Between(string attribute, string start, string end) => new("between", attribute, new List<string> { start, end }); | ||
|
||
public static Query Between(string attribute, int start, int end) => new("between", attribute, new List<int> { start, end }); | ||
|
||
public static Query Between(string attribute, double start, double end) => new("between", attribute, new List<double> { start, end }); | ||
|
||
public static Query Select(List<string> attributes) => new("select", null, attributes); | ||
|
||
public static Query CursorAfter(string documentId) => new("cursorAfter", null, documentId); | ||
|
||
public static Query CursorBefore(string documentId) => new("cursorBefore", null, documentId); | ||
|
||
public static Query OrderAsc(string attribute) => new("orderAsc", attribute, null); | ||
|
||
public static Query OrderDesc(string attribute) => new("orderDesc", attribute, null); | ||
|
||
public static Query Limit(int limit) => new("limit", null, limit); | ||
|
||
public static Query Offset(int offset) => new("offset", null, offset); | ||
|
||
public static Query Contains(string attribute, object value) => new("contains", attribute, value); | ||
|
||
public static Query Or(List<Query> queries) => new("or", null, queries); | ||
|
||
public static Query And(List<Query> queries) => new("and", null, queries); | ||
} |
82 changes: 82 additions & 0 deletions
82
tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.ListLogs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.Net; | ||
using PinguApps.Appwrite.Shared.Tests; | ||
using PinguApps.Appwrite.Shared.Utils; | ||
using RichardSzalay.MockHttp; | ||
|
||
namespace PinguApps.Appwrite.Client.Tests.Clients.Account; | ||
public partial class AccountClientTests | ||
{ | ||
[Fact] | ||
public async Task ListLogs_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/logs") | ||
.ExpectedHeaders(true) | ||
.Respond(Constants.AppJson, Constants.LogsListResponse); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.ListLogs(); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task ListLogs_ShouldProvideQueries_WhenQueriesProvided() | ||
{ | ||
// Arrange | ||
var query = Query.Limit(5); | ||
|
||
_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/logs") | ||
.ExpectedHeaders(true) | ||
.WithQueryString($"queries[]={query.GetQueryString()}") | ||
.Respond(Constants.AppJson, Constants.LogsListResponse); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.ListLogs([query]); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task ListLogs_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/logs") | ||
.ExpectedHeaders(true) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.ListLogs(); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task ListLogs_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/logs") | ||
.ExpectedHeaders(true) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.ListLogs(); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.