diff --git a/tests/Networking/NexusMods.Networking.NexusWebApi.Tests/OAuth2MessageFactoryTests.cs b/tests/Networking/NexusMods.Networking.NexusWebApi.Tests/OAuth2MessageFactoryTests.cs deleted file mode 100644 index 59423d2491..0000000000 --- a/tests/Networking/NexusMods.Networking.NexusWebApi.Tests/OAuth2MessageFactoryTests.cs +++ /dev/null @@ -1,113 +0,0 @@ -using Microsoft.Extensions.Logging; -using NexusMods.Common; -using System.Net; -using NexusMods.DataModel.Interprocess; -using NexusMods.DataModel.Abstractions; -using FluentAssertions; -using System.Text.Json; -using NexusMods.Common.OSInterop; -using NexusMods.DataModel.Interprocess.Jobs; -using NexusMods.Networking.NexusWebApi.DTOs.OAuth; -using NexusMods.Networking.NexusWebApi.NMA; -using NexusMods.Networking.NexusWebApi.NMA.Messages; -using NSubstitute; - -namespace NexusMods.Networking.NexusWebApi.Tests; - -public class OAuth2MessageFactoryTests -{ - private readonly OAuth2MessageFactory _factory; - private readonly IDataStore _store; - private readonly MockHttpMessageHandler _handler; - - public OAuth2MessageFactoryTests( - ILoggerFactory loggerFactory, - IMessageConsumer consumer, - IInterprocessJobManager jobManager) - { - _store = Substitute.For(); - - _handler = Substitute.ForPartsOf(); - var httpClient = new HttpClient(_handler); - - var idGen = Substitute.For(); - var os = Substitute.For(); - - var auth = new OAuth(loggerFactory.CreateLogger(), httpClient, idGen, os, consumer, jobManager); - _factory = new OAuth2MessageFactory(_store, auth); - } - - [Fact] - public async void AddsHeaderToRequest() - { - _store - .Get(JWTTokenEntity.StoreId, canCache: false) - .Returns(_ => new JWTTokenEntity - { - AccessToken = "access_token", - RefreshToken = "refresh_token", - ExpiresAt = DateTimeOffset.UtcNow - }); - - var request = await _factory.Create(HttpMethod.Get, new Uri("test://foobar")); - - request.Headers.Authorization.Should().NotBeNull(); - request.Headers.Authorization!.ToString().Should().Be("Bearer access_token"); - } - - [Fact()] - public async void ThrowsIfMissingToken() - { - Func> func = async () => await _factory.Create(HttpMethod.Get, new Uri("test://foobar")); - - await func.Should().ThrowAsync(); - } - - [Fact()] - public async void ForwardsUnrelatedErrors() - { - var msg = new HttpRequestMessage(HttpMethod.Get, "test://foobar"); - var ex = new HttpRequestException(); - var res = await _factory.HandleError(msg, ex, CancellationToken.None); - res.Should().BeNull(); - } - - [Fact()] - public async void RequestsRefreshOnTokenExpired() - { - _store - .Get(JWTTokenEntity.StoreId, canCache: false) - .Returns(_ => new JWTTokenEntity - { - AccessToken = "access_token", - RefreshToken = "refresh_token", - ExpiresAt = DateTimeOffset.UtcNow - }); - - _handler - .SendMock(Arg.Any(), Arg.Any()) - .Returns(Task.FromResult(new HttpResponseMessage - { - StatusCode = HttpStatusCode.OK, - Content = new StringContent(JsonSerializer.Serialize(RefreshToken)) - })); - - var msg = new HttpRequestMessage(HttpMethod.Get, "test://foobar"); - var ex = new HttpRequestException("Token has expired", null, HttpStatusCode.Unauthorized); - var res = await _factory.HandleError(msg, ex, CancellationToken.None); - res.Should().NotBeNull(); - res!.Headers.Authorization!.Should().NotBeNull(); - res.Headers.Authorization!.ToString().Should().Be("Bearer refreshed_access_token"); - } - - private static readonly JwtTokenReply RefreshToken = - new() - { - AccessToken = "refreshed_access_token", - RefreshToken = "refresh_token", - Scope = "public", - Type = "Bearer", - CreatedAt = 1677143380, - ExpiresIn = 21600, - }; -} diff --git a/tests/Networking/NexusMods.Networking.NexusWebApi.Tests/OAuthTests.cs b/tests/Networking/NexusMods.Networking.NexusWebApi.Tests/OAuthTests.cs index 81ad83c024..c518101505 100644 --- a/tests/Networking/NexusMods.Networking.NexusWebApi.Tests/OAuthTests.cs +++ b/tests/Networking/NexusMods.Networking.NexusWebApi.Tests/OAuthTests.cs @@ -180,12 +180,12 @@ public async void AuthorizationCanBeCanceled() }; // ReSharper disable once InconsistentNaming - private readonly Uri ExpectedAuthURL = new("https://users.nexusmods.com/oauth/authorize?response_type=code&scope=public&code_challenge_method=S256&client_id=vortex&redirect_uri=nxm%3A%2F%2Foauth%2Fcallback&code_challenge=-pSOp5xdZffKD0gc1lb5JALgN_ZtE9X573ib3yS8BT4&state=00000000-0000-0000-0000-000000000000"); + private readonly Uri ExpectedAuthURL = new("https://users.nexusmods.com/oauth/authorize?response_type=code&scope=openid profile email&code_challenge_method=S256&client_id=nma&redirect_uri=nxm%3A%2F%2Foauth%2Fcallback&code_challenge=QMZ4D7BLeehAXINE9NZ8dho2i5AYVTbfqJ8PhQ4eUrE&state=00000000-0000-0000-0000-000000000000"); [Fact] public void Test_GenerateAuthorizeUrl() { - var res = OAuth.GenerateAuthorizeUrl("-pSOp5xdZffKD0gc1lb5JALgN_ZtE9X573ib3yS8BT4", "00000000-0000-0000-0000-000000000000"); + var res = OAuth.GenerateAuthorizeUrl("QMZ4D7BLeehAXINE9NZ8dho2i5AYVTbfqJ8PhQ4eUrE", "00000000-0000-0000-0000-000000000000"); res.Should().Be(ExpectedAuthURL); } }