public async Task <EmbyToken> GetEmbyToken(EmbyLogin login) { if (!string.IsNullOrEmpty(login?.Password) && !string.IsNullOrEmpty(login.UserName)) { try { var token = await _embyClient.AuthenticateUserAsync(login.UserName, login.Password, login.Address); return(new EmbyToken { Token = token.AccessToken, Username = token.User.ConnectUserName, IsAdmin = token.User.Policy.IsAdministrator, Id = token.User.Id }); } catch (Exception e) { Log.Error("Username or password are wrong, user should try again with other credentials!"); Log.Error($"Message: {e.Message}"); throw new BusinessException("TOKEN_FAILED"); } } Log.Error("Username or password are empty, no use to try a login!"); throw new BusinessException("TOKEN_FAILED"); }
public async void GetEmbyTokenWithNoUserName() { var login = new EmbyLogin { Password = "******", Address = "http://localhost" }; BusinessException ex = await Assert.ThrowsAsync <BusinessException>(() => _subject.GetEmbyToken(login)); ex.Message.Should().Be("TOKEN_FAILED"); ex.StatusCode.Should().Be(500); }
public async void GetEmbyTokenFailedLogin() { _embyClientMock.Setup(x => x.AuthenticateUserAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .ThrowsAsync(new Exception()); var login = new EmbyLogin { Password = "******", Address = "http://localhost", UserName = "******" }; BusinessException ex = await Assert.ThrowsAsync <BusinessException>(() => _subject.GetEmbyToken(login)); ex.Message.Should().Be("TOKEN_FAILED"); ex.StatusCode.Should().Be(500); }
public async void GetEmbyToken() { _embyClientMock.Setup(x => x.AuthenticateUserAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(Task.FromResult(_authResult)); var login = new EmbyLogin { Address = "http://localhost", UserName = "******", Password = "******" }; var token = await _subject.GetEmbyToken(login); token.Username.Should().Be(_authResult.User.ConnectUserName); token.Token.Should().Be(_authResult.AccessToken); token.IsAdmin.Should().Be(_authResult.User.Policy.IsAdministrator); _embyClientMock.Verify(x => x.AuthenticateUserAsync( It.Is <string>(y => y == login.UserName), It.Is <string>(y => y == login.Password), It.Is <string>(y => y == login.Address))); }