using System.Linq;
using BVC.SpotBuyCenter.Core.Account;
using BVC.SpotBuyCenter.ServiceModel.Account;
using FluentAssertions;
using NUnit.Framework;
using ServiceStack.Html;
using ServiceStack.ServiceClient.Web;
using ServiceStack.ServiceInterface.Auth;
namespace BVC.SpotBuyCenter.Tests.Integration.Services
{
[TestFixture]
public class AccountServiceTests : IntegrationBaseTest
{
public UserAuth User { get; set; }
[Test]
public void Route_CaseNewDefault_Matches()
{
Host.TestRouteExists(new RouteRegistrationInfo("/role", typeof(Roles), new[] { HttpVerbs.Get }));
Host.TestRouteExists(new RouteRegistrationInfo("/account", typeof(Accounts), new[] { HttpVerbs.Get }));
Host.TestRouteExists(new RouteRegistrationInfo("/account/{Id}", typeof(Account), new[] { HttpVerbs.Get }));
Host.TestRouteExists(new RouteRegistrationInfo("/account/session", typeof(Session), new[] { HttpVerbs.Get }));
Host.TestRouteExists(new RouteRegistrationInfo("/account", typeof(SaveAccount), new[] { HttpVerbs.Post }));
Host.TestRouteExists(new RouteRegistrationInfo("/account/{Id}", typeof(SaveAccount), new[] { HttpVerbs.Put }));
Host.TestRouteExists(new RouteRegistrationInfo("/account/{Id}", typeof(DeleteAccount), new[] { HttpVerbs.Delete }));
}
[TearDown]
public void TearDown()
{
ClearDb();
}
[SetUp]
public void SetUp()
{
//TODO: remove this and test auth on services
User = Login(Role.SuperAdmin);
}
[Test]
public void Roles_ReturnsAllRoles()
{
var response = Client.Get(new Roles());
response.Roles.Should().NotBeNull();
response.Roles.Should().HaveCount(5);
Role.AllRoles.ForEach(role => response.Roles.Should().Contain(role));
}
[Test]
public void Accounts_SingleAccount_ReturnsOnlyThatAccount()
{
var response = Client.Get(new Accounts());
response.Accounts.Should().HaveCount(1);
var user = response.Accounts.Single();
user.Id.Should().Be(User.Id);
user.Email.Should().Be(User.Email);
user.Role.Should().Be(User.Roles.Single());
}
[Test]
public void Accounts_MultipleAccounts_ReturnsAllAccounts()
{
var user2 = new UserAuth{ Email = "bla@example.com" };
AuthRepository.CreateUserAuth(user2, "pass");
var response = Client.Get(new Accounts());
response.Accounts.Should().HaveCount(2);
var accountIds = response.Accounts.Select(x => x.Id);
accountIds.Should().Contain(User.Id);
accountIds.Should().Contain(user2.Id);
}
[Test]
public void Account_ReturnsAccount()
{
var response = Client.Get(new Account { Id = User.Id });
response.Account.Should().NotBeNull();
response.Account.Id.Should().Be(User.Id);
response.Account.Email.Should().Be(User.Email);
response.Account.Role.Should().Be(User.Roles.Single());
response.Account.FirstName.Should().Be(User.FirstName);
response.Account.LastName.Should().Be(User.LastName);
}
[Test]
[ExpectedException(typeof(WebServiceException ), ExpectedMessage = "No account with this id found.")]
public void Account_AccountDoesntExist_404()
{
Client.Get(new Account { Id = -1 });
}
//Untestable since real requests are made in a different way?
//[Test]
//public void Session_ReturnsCurrentUserSessionInfo()
//{
// var response = Client.Get(new Session());
// response.Session.Should().NotBeNull();
// response.Session.Id.Should().Be(User.Id);
// response.Session.Email.Should().Be(User.Email);
// response.Session.FirstName.Should().Be(User.FirstName);
// response.Session.LastName.Should().Be(User.LastName);
// response.Session.Role.Should().Be(User.Roles.First());
//}
[Test]
public void SaveAccount_Post_SavesNewAccount()
{
var request = new SaveAccount {
Email = "foo@bar.com",
FirstName = "foo",
LastName = "bar",
NewPassword = "pass",
NewPasswordConfirm = "pass",
Role = Role.SuperAdmin
};
var response = Client.Post(request);
using (var session = Store.OpenSession())
{
var loadedAccount = session.Load<UserAuth>(response.Account.Id);
loadedAccount.Should().NotBeNull();
loadedAccount.Email.Should().Be(request.Email);
loadedAccount.FirstName.Should().Be(request.FirstName);
loadedAccount.LastName.Should().Be(request.LastName);
loadedAccount.Roles.Should().Contain(request.Role);
}
}
[Test]
public void SaveAccount_Put_UpdatesExistingAccount()
{
var request = new SaveAccount
{
Id = User.Id,
Email = "foo@bar.com",
FirstName = "foo",
LastName = "bar",
NewPassword = "pass",
NewPasswordConfirm = "pass",
Role = Role.SuperAdmin
};
Client.Put(request);
using (var session = Store.OpenSession())
{
var loadedAccount = session.Load<UserAuth>(User.Id);
loadedAccount.Should().NotBeNull();
loadedAccount.Email.Should().Be(request.Email);
loadedAccount.FirstName.Should().Be(request.FirstName);
loadedAccount.LastName.Should().Be(request.LastName);
loadedAccount.Roles.Should().Contain(request.Role);
}
}
[Test]
[ExpectedException(typeof(WebServiceException), ExpectedMessage = "No account with this id found.")]
public void SaveAccount_Put_NoAccountWithThisId_404()
{
var request = new SaveAccount
{
Id = -1,
Email = "foo@bar.com",
FirstName = "foo",
LastName = "bar",
NewPassword = "pass",
NewPasswordConfirm = "pass",
Role = Role.SuperAdmin
};
Client.Put(request);
}
[Test]
public void DeleteAccount_DeletesAccount()
{
var request = new DeleteAccount {
Id = User.Id
};
Client.Delete(request);
using (var session = Store.OpenSession())
{
var loadedAccount = session.Load<UserAuth>(User.Id);
loadedAccount.Should().BeNull();
}
}
[Test]
[ExpectedException(typeof(WebServiceException), ExpectedMessage = "No account with this id found.")]
public void DeleteAccount_AccountDoesntExist_404()
{
var request = new DeleteAccount { Id = -1 };
Client.Delete(request);
}
}
}