Skip to content

Commit

Permalink
add login service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
byCrookie committed Oct 28, 2023
1 parent 25d5e82 commit b7947ea
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Logged in as test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Logged in as test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Logged in as test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emptyString
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
using FluentAssertions;
using GithubBackup.Cli.Commands.Github.Auth;
using GithubBackup.Cli.Commands.Github.Auth.Pipeline;
using GithubBackup.Cli.Commands.Github.Login;
using GithubBackup.Cli.Commands.Global;
using GithubBackup.Core.Github.Users;
using GithubBackup.TestUtils.Logging;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Spectre.Console.Testing;

namespace GithubBackup.Cli.Tests.Commands.Github.Auth;

[UsesVerify]
public class LoginServiceTests
{
private readonly LoginService _sut;
private readonly ILogger<LoginService> _logger;
private readonly TestConsole _ansiConsole;
private readonly ILoginPipelineBuilder _loginPipelineBuilder;

public LoginServiceTests()
{
_logger = Substitute.For<ILogger<LoginService>>();
_ansiConsole = new TestConsole();
_loginPipelineBuilder = Substitute.For<ILoginPipelineBuilder>();

_sut = new LoginService(
_logger,
_ansiConsole,
_loginPipelineBuilder
);
}

[Fact]
public async Task PersistentOnlyAsync_NotQuietAndNoUser_ReturnNullAndNotPrint()
{
var globalArgs = new GlobalArgs(LogLevel.Debug, false, new FileInfo("Test"));
var args = new LoginArgs(null, false);

var pipeline = Substitute.For<ILoginPipeline>();
_loginPipelineBuilder.PersistedOnly().Returns(pipeline);
pipeline.LoginAsync(globalArgs, args, false, CancellationToken.None).Returns((User?)null);

var result = await _sut.PersistentOnlyAsync(globalArgs, args, CancellationToken.None);

result.Should().BeNull();

_logger.VerifyLogs();
await Verify(_ansiConsole.Output);
}

[Fact]
public async Task PersistentOnlyAsync_NotQuietAndUser_ReturnUserAndPrint()
{
var globalArgs = new GlobalArgs(LogLevel.Debug, false, new FileInfo("Test"));
var args = new LoginArgs(null, false);
var user = new User("test", "test");

var pipeline = Substitute.For<ILoginPipeline>();
_loginPipelineBuilder.PersistedOnly().Returns(pipeline);
pipeline.LoginAsync(globalArgs, args, false, CancellationToken.None).Returns(user);

var result = await _sut.PersistentOnlyAsync(globalArgs, args, CancellationToken.None);

result.Should().Be(user);

_logger.VerifyLogs(new LogEntry(LogLevel.Information, "Logged in as test"));
await Verify(_ansiConsole.Output);
}

[Fact]
public async Task PersistentOnlyAsync_QuietAndUser_ReturnUserAndDoNotPrint()
{
var globalArgs = new GlobalArgs(LogLevel.Debug, true, new FileInfo("Test"));
var args = new LoginArgs(null, false);
var user = new User("test", "test");

var pipeline = Substitute.For<ILoginPipeline>();
_loginPipelineBuilder.PersistedOnly().Returns(pipeline);
pipeline.LoginAsync(globalArgs, args, false, CancellationToken.None).Returns(user);

var result = await _sut.PersistentOnlyAsync(globalArgs, args, CancellationToken.None);

result.Should().Be(user);

_logger.VerifyLogs();
await Verify(_ansiConsole.Output);
}

[Fact]
public async Task WithoutPersistentAsync_NotQuietAndNoUser_ReturnNullAndNotPrint()
{
var globalArgs = new GlobalArgs(LogLevel.Debug, false, new FileInfo("Test"));
var args = new LoginArgs(null, false);

var pipeline = Substitute.For<ILoginPipeline>();
_loginPipelineBuilder.WithoutPersistent().Returns(pipeline);
pipeline.LoginAsync(globalArgs, args, false, CancellationToken.None).Returns((User?)null);

var action = () => _sut.WithoutPersistentAsync(globalArgs, args, false, CancellationToken.None);

await action.Should().ThrowAsync<Exception>();

_logger.VerifyLogs();
await Verify(_ansiConsole.Output);
}

[Fact]
public async Task WithoutPersistentAsync_NotQuietAndUser_ReturnUserAndPrint()
{
var globalArgs = new GlobalArgs(LogLevel.Debug, false, new FileInfo("Test"));
var args = new LoginArgs(null, false);
var user = new User("test", "test");

var pipeline = Substitute.For<ILoginPipeline>();
_loginPipelineBuilder.WithoutPersistent().Returns(pipeline);
pipeline.LoginAsync(globalArgs, args, false, CancellationToken.None).Returns(user);

var result = await _sut.WithoutPersistentAsync(globalArgs, args, false, CancellationToken.None);

result.Should().Be(user);

_logger.VerifyLogs(new LogEntry(LogLevel.Information, "Logged in as test"));
await Verify(_ansiConsole.Output);
}

[Fact]
public async Task WithoutPersistentAsync_QuietAndUser_ReturnUserAndDoNotPrint()
{
var globalArgs = new GlobalArgs(LogLevel.Debug, true, new FileInfo("Test"));
var args = new LoginArgs(null, false);
var user = new User("test", "test");

var pipeline = Substitute.For<ILoginPipeline>();
_loginPipelineBuilder.WithoutPersistent().Returns(pipeline);
pipeline.LoginAsync(globalArgs, args, false, CancellationToken.None).Returns(user);

var result = await _sut.WithoutPersistentAsync(globalArgs, args, false, CancellationToken.None);

result.Should().Be(user);

_logger.VerifyLogs();
await Verify(_ansiConsole.Output);
}

[Fact]
public async Task WithPersistentAsync_NotQuietAndNoUser_ReturnNullAndNotPrint()
{
var globalArgs = new GlobalArgs(LogLevel.Debug, false, new FileInfo("Test"));
var args = new LoginArgs(null, false);

var pipeline = Substitute.For<ILoginPipeline>();
_loginPipelineBuilder.WithPersistent().Returns(pipeline);
pipeline.LoginAsync(globalArgs, args, false, CancellationToken.None).Returns((User?)null);

var action = () => _sut.WithPersistentAsync(globalArgs, args, false, CancellationToken.None);

await action.Should().ThrowAsync<Exception>();

_logger.VerifyLogs();
await Verify(_ansiConsole.Output);
}

[Fact]
public async Task WithPersistentAsync_NotQuietAndUser_ReturnUserAndPrint()
{
var globalArgs = new GlobalArgs(LogLevel.Debug, false, new FileInfo("Test"));
var args = new LoginArgs(null, false);
var user = new User("test", "test");

var pipeline = Substitute.For<ILoginPipeline>();
_loginPipelineBuilder.WithPersistent().Returns(pipeline);
pipeline.LoginAsync(globalArgs, args, false, CancellationToken.None).Returns(user);

var result = await _sut.WithPersistentAsync(globalArgs, args, false, CancellationToken.None);

result.Should().Be(user);

_logger.VerifyLogs(new LogEntry(LogLevel.Information, "Logged in as test"));
await Verify(_ansiConsole.Output);
}

[Fact]
public async Task WithPersistentAsync_QuietAndUser_ReturnUserAndDoNotPrint()
{
var globalArgs = new GlobalArgs(LogLevel.Debug, true, new FileInfo("Test"));
var args = new LoginArgs(null, false);
var user = new User("test", "test");

var pipeline = Substitute.For<ILoginPipeline>();
_loginPipelineBuilder.WithPersistent().Returns(pipeline);
pipeline.LoginAsync(globalArgs, args, false, CancellationToken.None).Returns(user);

var result = await _sut.WithPersistentAsync(globalArgs, args, false, CancellationToken.None);

result.Should().Be(user);

_logger.VerifyLogs();
await Verify(_ansiConsole.Output);
}
}

0 comments on commit b7947ea

Please sign in to comment.