-
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 #78 from PinguApps/44-update-session-implementation
update session implementation
- Loading branch information
Showing
9 changed files
with
261 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,28 @@ public async Task Run(string[] args) | |
|
||
Console.WriteLine("Getting Session..."); | ||
|
||
var account = await _client.Account.GetSession("6695d717983fadf6ece1"); | ||
//var response = await _client.Account.CreateEmailToken(new CreateEmailTokenRequest | ||
//{ | ||
// Email = "[email protected]", | ||
// UserId = "664aac1a00113f82e620" | ||
//}); | ||
|
||
Console.WriteLine(account.Result.Match( | ||
//var response = await _client.Account.CreateSession(new CreateSessionRequest | ||
//{ | ||
// UserId = "664aac1a00113f82e620", | ||
// Secret = "623341" | ||
//}); | ||
|
||
var response = await _client.Account.GetSession("66a810f2e55b1329e25b"); | ||
|
||
var response2 = await _client.Account.UpdateSession("66a810f2e55b1329e25b"); | ||
|
||
Console.WriteLine(response.Result.Match( | ||
account => account.ToString(), | ||
appwriteError => appwriteError.Message, | ||
internalERror => internalERror.Message)); | ||
|
||
Console.WriteLine(response2.Result.Match( | ||
account => account.ToString(), | ||
appwriteError => appwriteError.Message, | ||
internalERror => internalERror.Message)); | ||
|
40 changes: 40 additions & 0 deletions
40
src/PinguApps.Appwrite.Shared/Converters/MultiFormatDateTimeConverter .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,40 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PinguApps.Appwrite.Shared.Converters; | ||
|
||
/// <summary> | ||
/// This is only required temporarily as a workaround for #8447 on Appwrite. | ||
/// <para><see href="https://github.com/appwrite/appwrite/issues/8447"/></para> | ||
/// </summary> | ||
public class MultiFormatDateTimeConverter : JsonConverter<DateTime> | ||
{ | ||
private readonly string[] _formats = [ | ||
"yyyy-MM-ddTHH:mm:ss.fffK", | ||
"yyyy-MM-dd HH:mm:ss.fff" | ||
]; | ||
|
||
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.String) | ||
{ | ||
var dateString = reader.GetString(); | ||
|
||
foreach (var format in _formats) | ||
{ | ||
if (DateTime.TryParseExact(dateString, format, null, System.Globalization.DateTimeStyles.None, out var dateTime)) | ||
{ | ||
return dateTime; | ||
} | ||
} | ||
throw new JsonException($"Unable to parse date: {dateString}"); | ||
} | ||
throw new JsonException("Invalid token type"); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.ToString(_formats[0])); // Use the first format for serialization | ||
} | ||
} |
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
81 changes: 81 additions & 0 deletions
81
tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.UpdateSession.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,81 @@ | ||
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 UpdateSession_HitsCurrent_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/sessions/current") | ||
.ExpectedHeaders(true) | ||
.Respond(Constants.AppJson, Constants.UserResponse); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdateSession(); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdateSession_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var sessionId = IdUtils.GenerateUniqueId(); | ||
|
||
_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/sessions/{sessionId}") | ||
.ExpectedHeaders(true) | ||
.Respond(Constants.AppJson, Constants.UserResponse); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdateSession(sessionId); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdateSession_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/sessions/current") | ||
.ExpectedHeaders(true) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdateSession(); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdateSession_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/sessions/current") | ||
.ExpectedHeaders(true) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdateSession(); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
tests/PinguApps.Appwrite.Shared.Tests/Converters/MultiFormatDateTimeConverterTests.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,88 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using PinguApps.Appwrite.Shared.Converters; | ||
|
||
namespace PinguApps.Appwrite.Shared.Tests.Converters; | ||
public class MultiFormatDateTimeConverterTests | ||
{ | ||
private readonly JsonSerializerOptions _options; | ||
|
||
public MultiFormatDateTimeConverterTests() | ||
{ | ||
_options = new JsonSerializerOptions(); | ||
_options.Converters.Add(new MultiFormatDateTimeConverter()); | ||
} | ||
|
||
[Fact] | ||
public void Read_ValidDateStringWithTimeZone_ReturnsDateTime() | ||
{ | ||
var json = "\"2023-01-01T00:00:00.000Z\""; | ||
var result = JsonSerializer.Deserialize<DateTime>(json, _options); | ||
|
||
// Convert both to UTC to compare | ||
var expectedDateTime = new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
var actualDateTime = result.ToUniversalTime(); | ||
|
||
Assert.Equal(expectedDateTime, actualDateTime); | ||
} | ||
|
||
[Fact] | ||
public void Read_ValidDateStringWithoutTimeZone_ReturnsDateTime() | ||
{ | ||
var json = "\"2023-01-01 00:00:00.000\""; | ||
var result = JsonSerializer.Deserialize<DateTime>(json, _options); | ||
Assert.Equal(new DateTime(2023, 1, 1, 0, 0, 0), result); | ||
} | ||
|
||
[Fact] | ||
public void Read_InvalidDateString_ThrowsJsonException() | ||
{ | ||
var json = "\"invalid-date\""; | ||
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<DateTime>(json, _options)); | ||
} | ||
|
||
[Fact] | ||
public void Read_UnexpectedTokenType_ThrowsJsonException() | ||
{ | ||
var json = "123"; | ||
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<DateTime>(json, _options)); | ||
} | ||
|
||
[Fact] | ||
public void Write_ValidDateTime_WritesExpectedString() | ||
{ | ||
var dateTime = new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
var json = JsonSerializer.Serialize(dateTime, _options); | ||
Assert.Equal("\"2023-01-01T00:00:00.000Z\"", json); | ||
} | ||
|
||
public class MultiFormatDateTimeObject | ||
{ | ||
[JsonPropertyName("x")] | ||
[JsonConverter(typeof(MultiFormatDateTimeConverter))] | ||
public DateTime X { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void Read_ValidDateStringInObject_ReturnsDateTime() | ||
{ | ||
var json = "{\"x\": \"2023-01-01T00:00:00.000Z\"}"; | ||
var result = JsonSerializer.Deserialize<MultiFormatDateTimeObject>(json, _options); | ||
Assert.NotNull(result); | ||
|
||
|
||
// Convert both to UTC to compare | ||
var expectedDateTime = new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
var actualDateTime = result.X.ToUniversalTime(); | ||
|
||
Assert.Equal(expectedDateTime, actualDateTime); | ||
} | ||
|
||
[Fact] | ||
public void Write_ValidDateTimeInObject_WritesExpectedString() | ||
{ | ||
var obj = new MultiFormatDateTimeObject { X = new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc) }; | ||
var json = JsonSerializer.Serialize(obj, _options); | ||
Assert.Equal("{\"x\":\"2023-01-01T00:00:00.000Z\"}", json); | ||
} | ||
} |