|
|
|
@ -36,7 +36,6 @@ using Newtonsoft.Json;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
using PlexRequests.Api.Interfaces;
|
|
|
|
|
using PlexRequests.Api.Models;
|
|
|
|
|
using PlexRequests.Api.Models.Plex;
|
|
|
|
|
using PlexRequests.Core;
|
|
|
|
|
using PlexRequests.Core.SettingModels;
|
|
|
|
@ -48,57 +47,254 @@ namespace PlexRequests.UI.Tests
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class AdminModuleTests
|
|
|
|
|
{
|
|
|
|
|
private Mock<ISettingsService<PlexRequestSettings>> PlexRequestMock { get; set; }
|
|
|
|
|
private Mock<ISettingsService<CouchPotatoSettings>> CpMock { get; set; }
|
|
|
|
|
private Mock<ISettingsService<AuthenticationSettings>> AuthMock { get; set; }
|
|
|
|
|
private Mock<ISettingsService<PlexSettings>> PlexSettingsMock { get; set; }
|
|
|
|
|
private Mock<ISettingsService<SonarrSettings>> SonarrSettingsMock { get; set; }
|
|
|
|
|
private Mock<ISettingsService<EmailNotificationSettings>> EmailMock { get; set; }
|
|
|
|
|
private Mock<IPlexApi> PlexMock { get; set; }
|
|
|
|
|
private Mock<ISonarrApi> SonarrApiMock { get; set; }
|
|
|
|
|
|
|
|
|
|
private ConfigurableBootstrapper Bootstrapper { get; set; }
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
|
|
|
|
AuthMock = new Mock<ISettingsService<AuthenticationSettings>>();
|
|
|
|
|
var expectedSettings = new AuthenticationSettings { UserAuthentication = false, PlexAuthToken = "abc" };
|
|
|
|
|
AuthMock.Setup(x => x.GetSettings()).Returns(expectedSettings);
|
|
|
|
|
|
|
|
|
|
PlexMock = new Mock<IPlexApi>();
|
|
|
|
|
}
|
|
|
|
|
PlexMock.Setup(x => x.SignIn("Username1", "Password1"))
|
|
|
|
|
.Returns(new PlexAuthentication { user = new User { authentication_token = "abc", username = "Username1" } });
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
[Ignore("Need to finish")]
|
|
|
|
|
public void RequestAuthTokenTest()
|
|
|
|
|
{
|
|
|
|
|
var expectedSettings = new AuthenticationSettings {UserAuthentication = false, PlexAuthToken = "abc"};
|
|
|
|
|
AuthMock.Setup(x => x.GetSettings()).Returns(expectedSettings);
|
|
|
|
|
PlexRequestMock = new Mock<ISettingsService<PlexRequestSettings>>();
|
|
|
|
|
CpMock = new Mock<ISettingsService<CouchPotatoSettings>>();
|
|
|
|
|
PlexSettingsMock = new Mock<ISettingsService<PlexSettings>>();
|
|
|
|
|
SonarrApiMock = new Mock<ISonarrApi>();
|
|
|
|
|
SonarrSettingsMock = new Mock<ISettingsService<SonarrSettings>>();
|
|
|
|
|
EmailMock = new Mock<ISettingsService<EmailNotificationSettings>>();
|
|
|
|
|
|
|
|
|
|
var bootstrapper = new ConfigurableBootstrapper(with =>
|
|
|
|
|
Bootstrapper = new ConfigurableBootstrapper(with =>
|
|
|
|
|
{
|
|
|
|
|
with.Module<UserLoginModule>();
|
|
|
|
|
with.Module<AdminModule>();
|
|
|
|
|
with.Dependency(AuthMock.Object);
|
|
|
|
|
with.Dependency(PlexRequestMock.Object);
|
|
|
|
|
with.Dependency(CpMock.Object);
|
|
|
|
|
with.Dependency(PlexSettingsMock.Object);
|
|
|
|
|
with.Dependency(SonarrApiMock.Object);
|
|
|
|
|
with.Dependency(SonarrSettingsMock.Object);
|
|
|
|
|
with.Dependency(PlexMock.Object);
|
|
|
|
|
with.Dependency(EmailMock.Object);
|
|
|
|
|
with.RootPathProvider<TestRootPathProvider>();
|
|
|
|
|
with.RequestStartup((container, pipelines, context) =>
|
|
|
|
|
{
|
|
|
|
|
context.CurrentUser = new UserIdentity { UserName = "user"};
|
|
|
|
|
context.CurrentUser = new UserIdentity { UserName = "user" };
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
bootstrapper.WithSession(new Dictionary<string, object>());
|
|
|
|
|
Bootstrapper.WithSession(new Dictionary<string, object>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var browser = new Browser(bootstrapper);
|
|
|
|
|
[Test]
|
|
|
|
|
public void RequestAuthTokenTestNewSettings()
|
|
|
|
|
{
|
|
|
|
|
var browser = new Browser(Bootstrapper);
|
|
|
|
|
|
|
|
|
|
var result = browser.Post("/admin/requestauth", with =>
|
|
|
|
|
{
|
|
|
|
|
with.HttpRequest();
|
|
|
|
|
with.Header("Accept", "application/json");
|
|
|
|
|
with.FormValue("username", "abc");
|
|
|
|
|
with.FormValue("password","pass");
|
|
|
|
|
with.FormValue("username", "Username1");
|
|
|
|
|
with.FormValue("password", "Password1");
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode));
|
|
|
|
|
Assert.That(result.Context.Request.Session[SessionKeys.UsernameKey], Is.EqualTo("abc"));
|
|
|
|
|
|
|
|
|
|
var body = JsonConvert.DeserializeObject<JsonResponseModel>(result.Body.AsString());
|
|
|
|
|
Assert.That(body.Result, Is.EqualTo(true));
|
|
|
|
|
PlexMock.Verify(x => x.SignIn("Username1", "Password1"), Times.Once);
|
|
|
|
|
AuthMock.Verify(x => x.GetSettings(), Times.Once);
|
|
|
|
|
PlexMock.Verify(x => x.SignIn(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
|
|
|
|
PlexMock.Verify(x => x.GetUsers(It.IsAny<string>()), Times.Never);
|
|
|
|
|
AuthMock.Verify(x => x.SaveSettings(It.IsAny<AuthenticationSettings>()), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void RequestAuthTokenTestEmptyCredentials()
|
|
|
|
|
{
|
|
|
|
|
var browser = new Browser(Bootstrapper);
|
|
|
|
|
|
|
|
|
|
var result = browser.Post("/admin/requestauth", with =>
|
|
|
|
|
{
|
|
|
|
|
with.HttpRequest();
|
|
|
|
|
with.Header("Accept", "application/json");
|
|
|
|
|
with.FormValue("username", string.Empty);
|
|
|
|
|
with.FormValue("password", "Password1");
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode));
|
|
|
|
|
|
|
|
|
|
var body = JsonConvert.DeserializeObject<JsonResponseModel>(result.Body.AsString());
|
|
|
|
|
Assert.That(body.Result, Is.EqualTo(false));
|
|
|
|
|
Assert.That(body.Message, Is.Not.Empty);
|
|
|
|
|
|
|
|
|
|
PlexMock.Verify(x => x.SignIn("Username1", "Password1"), Times.Never);
|
|
|
|
|
AuthMock.Verify(x => x.GetSettings(), Times.Never);
|
|
|
|
|
AuthMock.Verify(x => x.SaveSettings(It.IsAny<AuthenticationSettings>()), Times.Never);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void RequestAuthTokenTesPlexSignInFail()
|
|
|
|
|
{
|
|
|
|
|
var browser = new Browser(Bootstrapper);
|
|
|
|
|
|
|
|
|
|
var result = browser.Post("/admin/requestauth", with =>
|
|
|
|
|
{
|
|
|
|
|
with.HttpRequest();
|
|
|
|
|
with.Header("Accept", "application/json");
|
|
|
|
|
with.FormValue("username", "Badusername");
|
|
|
|
|
with.FormValue("password", "Password1");
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode));
|
|
|
|
|
|
|
|
|
|
var body = JsonConvert.DeserializeObject<JsonResponseModel>(result.Body.AsString());
|
|
|
|
|
Assert.That(body.Result, Is.EqualTo(false));
|
|
|
|
|
Assert.That(body.Message, Is.Not.Empty);
|
|
|
|
|
|
|
|
|
|
PlexMock.Verify(x => x.SignIn("Badusername", "Password1"), Times.Once);
|
|
|
|
|
AuthMock.Verify(x => x.GetSettings(), Times.Never);
|
|
|
|
|
AuthMock.Verify(x => x.SaveSettings(It.IsAny<AuthenticationSettings>()), Times.Never);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void RequestAuthTokenTestExistingSettings()
|
|
|
|
|
{
|
|
|
|
|
AuthMock.Setup(x => x.GetSettings()).Returns(() => null);
|
|
|
|
|
var browser = new Browser(Bootstrapper);
|
|
|
|
|
|
|
|
|
|
var result = browser.Post("/admin/requestauth", with =>
|
|
|
|
|
{
|
|
|
|
|
with.HttpRequest();
|
|
|
|
|
with.Header("Accept", "application/json");
|
|
|
|
|
with.FormValue("username", "Username1");
|
|
|
|
|
with.FormValue("password", "Password1");
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode));
|
|
|
|
|
|
|
|
|
|
var body = JsonConvert.DeserializeObject<JsonResponseModel>(result.Body.AsString());
|
|
|
|
|
Assert.That(body.Result, Is.EqualTo(true));
|
|
|
|
|
|
|
|
|
|
PlexMock.Verify(x => x.SignIn("Username1", "Password1"), Times.Once);
|
|
|
|
|
AuthMock.Verify(x => x.GetSettings(), Times.Once);
|
|
|
|
|
AuthMock.Verify(x => x.SaveSettings(It.IsAny<AuthenticationSettings>()), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void GetUsersSuccessfully()
|
|
|
|
|
{
|
|
|
|
|
var users = new PlexFriends { User = new[] { new UserFriends { Username = "abc2" }, } };
|
|
|
|
|
PlexMock.Setup(x => x.GetUsers(It.IsAny<string>())).Returns(users);
|
|
|
|
|
var browser = new Browser(Bootstrapper);
|
|
|
|
|
|
|
|
|
|
var result = browser.Get("/admin/getusers", with =>
|
|
|
|
|
{
|
|
|
|
|
with.HttpRequest();
|
|
|
|
|
with.Header("Accept", "application/json");
|
|
|
|
|
with.FormValue("username", "Username1");
|
|
|
|
|
with.FormValue("password", "Password1");
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode));
|
|
|
|
|
|
|
|
|
|
var body = result.Body.AsString();
|
|
|
|
|
Assert.That(body, Is.Not.Null);
|
|
|
|
|
Assert.That(body, Contains.Substring("abc2"));
|
|
|
|
|
|
|
|
|
|
PlexMock.Verify(x => x.GetUsers(It.IsAny<string>()), Times.Once);
|
|
|
|
|
AuthMock.Verify(x => x.GetSettings(), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void GetUsersReturnsNoUsers()
|
|
|
|
|
{
|
|
|
|
|
var users = new PlexFriends();
|
|
|
|
|
PlexMock.Setup(x => x.GetUsers(It.IsAny<string>())).Returns(users);
|
|
|
|
|
var browser = new Browser(Bootstrapper);
|
|
|
|
|
|
|
|
|
|
var result = browser.Get("/admin/getusers", with =>
|
|
|
|
|
{
|
|
|
|
|
with.HttpRequest();
|
|
|
|
|
with.Header("Accept", "application/json");
|
|
|
|
|
with.FormValue("username", "Username1");
|
|
|
|
|
with.FormValue("password", "Password1");
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode));
|
|
|
|
|
|
|
|
|
|
var body = JsonConvert.DeserializeObject<string>(result.Body.AsString());
|
|
|
|
|
Assert.That(body, Is.Not.Null);
|
|
|
|
|
Assert.That(string.IsNullOrWhiteSpace(body), Is.True);
|
|
|
|
|
|
|
|
|
|
PlexMock.Verify(x => x.GetUsers(It.IsAny<string>()), Times.Once);
|
|
|
|
|
AuthMock.Verify(x => x.GetSettings(), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void GetUsersReturnsNull()
|
|
|
|
|
{
|
|
|
|
|
PlexMock.Setup(x => x.GetUsers(It.IsAny<string>())).Returns(() => null);
|
|
|
|
|
var browser = new Browser(Bootstrapper);
|
|
|
|
|
|
|
|
|
|
var result = browser.Get("/admin/getusers", with =>
|
|
|
|
|
{
|
|
|
|
|
with.HttpRequest();
|
|
|
|
|
with.Header("Accept", "application/json");
|
|
|
|
|
with.FormValue("username", "Username1");
|
|
|
|
|
with.FormValue("password", "Password1");
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode));
|
|
|
|
|
|
|
|
|
|
var body = JsonConvert.DeserializeObject<string>(result.Body.AsString());
|
|
|
|
|
Assert.That(body, Is.Not.Null);
|
|
|
|
|
Assert.That(string.IsNullOrWhiteSpace(body), Is.True);
|
|
|
|
|
|
|
|
|
|
PlexMock.Verify(x => x.GetUsers(It.IsAny<string>()), Times.Once);
|
|
|
|
|
AuthMock.Verify(x => x.GetSettings(), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void GetUsersTokenIsNull()
|
|
|
|
|
{
|
|
|
|
|
AuthMock.Setup(x => x.GetSettings()).Returns(new AuthenticationSettings());
|
|
|
|
|
var browser = new Browser(Bootstrapper);
|
|
|
|
|
|
|
|
|
|
var result = browser.Get("/admin/getusers", with =>
|
|
|
|
|
{
|
|
|
|
|
with.HttpRequest();
|
|
|
|
|
with.Header("Accept", "application/json");
|
|
|
|
|
with.FormValue("username", "Username1");
|
|
|
|
|
with.FormValue("password", "Password1");
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode));
|
|
|
|
|
|
|
|
|
|
var body = JsonConvert.DeserializeObject<string>(result.Body.AsString());
|
|
|
|
|
Assert.That(body, Is.Not.Null);
|
|
|
|
|
Assert.That(string.IsNullOrWhiteSpace(body), Is.True);
|
|
|
|
|
|
|
|
|
|
PlexMock.Verify(x => x.GetUsers(It.IsAny<string>()), Times.Never);
|
|
|
|
|
AuthMock.Verify(x => x.GetSettings(), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|