From ad7b5af9979baee0ef3bf270b6319007da992aad Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Tue, 9 Apr 2019 16:30:42 +0100 Subject: [PATCH] More unit tests --- .../Authentication/OmbiUserManagerTests.cs | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/Ombi.Core.Tests/Authentication/OmbiUserManagerTests.cs diff --git a/src/Ombi.Core.Tests/Authentication/OmbiUserManagerTests.cs b/src/Ombi.Core.Tests/Authentication/OmbiUserManagerTests.cs new file mode 100644 index 000000000..bd6e8d5a2 --- /dev/null +++ b/src/Ombi.Core.Tests/Authentication/OmbiUserManagerTests.cs @@ -0,0 +1,106 @@ +using Microsoft.AspNetCore.Identity; +using Moq; +using NUnit.Framework; +using Ombi.Api.Plex; +using Ombi.Api.Plex.Models; +using Ombi.Core.Authentication; +using Ombi.Core.Settings; +using Ombi.Settings.Settings.Models; +using Ombi.Store.Entities; +using Ombi.Test.Common; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Ombi.Core.Tests.Authentication +{ + [TestFixture] + public class OmbiUserManagerTests + { + + [SetUp] + public void Setup() + { + + UserStore = new Mock>(); + PlexApi = new Mock(); + AuthenticationSettings = new Mock>(); + + AuthenticationSettings.Setup(x => x.GetSettingsAsync()) + .ReturnsAsync(new AuthenticationSettings()); + _um = new OmbiUserManager(UserStore.Object, null, null, null, null, null, null, null, null, + PlexApi.Object, null, null, AuthenticationSettings.Object); + } + + public OmbiUserManager _um { get; set; } + private Mock> UserStore { get; set; } + private Mock PlexApi { get; set; } + private Mock> AuthenticationSettings { get; set; } + + [Test] + public async Task CheckPassword_PlexUser_EmailLogin_ValidPassword() + { + var user = new OmbiUser + { + UserType = UserType.PlexUser, + EmailLogin = true, + Email = "MyEmail@email.com" + }; + PlexApi.Setup(x => x.SignIn(It.IsAny())) + .ReturnsAsync(new PlexAuthentication + { + user = new User + { + authentication_token = "abc" + } + }); + var result = await _um.CheckPasswordAsync(user, "pass"); + + Assert.That(result, Is.True); + PlexApi.Verify(x => x.SignIn(It.Is(c => c.login == "MyEmail@email.com")), Times.Once); + } + + [Test] + public async Task CheckPassword_PlexUser_UserNameLogin_ValidPassword() + { + var user = new OmbiUser + { + UserType = UserType.PlexUser, + EmailLogin = false, + Email = "MyEmail@email.com", + UserName = "heyhey" + }; + PlexApi.Setup(x => x.SignIn(It.IsAny())) + .ReturnsAsync(new PlexAuthentication + { + user = new User + { + authentication_token = "abc" + } + }); + var result = await _um.CheckPasswordAsync(user, "pass"); + + Assert.That(result, Is.True); + PlexApi.Verify(x => x.SignIn(It.Is(c => c.login == "heyhey")), Times.Once); + } + + [Test] + public async Task CheckPassword_PlexUser_UserNameLogin_InvalidPassword() + { + var user = new OmbiUser + { + UserType = UserType.PlexUser, + EmailLogin = false, + Email = "MyEmail@email.com", + UserName = "heyhey" + }; + PlexApi.Setup(x => x.SignIn(It.IsAny())) + .ReturnsAsync(new PlexAuthentication()); + var result = await _um.CheckPasswordAsync(user, "pass"); + + Assert.That(result, Is.False); + PlexApi.Verify(x => x.SignIn(It.Is(c => c.login == "heyhey")), Times.Once); + } + } +}