fork download
  1. using System.Linq;
  2. using BVC.SpotBuyCenter.Core.Account;
  3. using BVC.SpotBuyCenter.ServiceModel.Account;
  4. using FluentAssertions;
  5. using NUnit.Framework;
  6. using ServiceStack.Html;
  7. using ServiceStack.ServiceClient.Web;
  8. using ServiceStack.ServiceInterface.Auth;
  9.  
  10. namespace BVC.SpotBuyCenter.Tests.Integration.Services
  11. {
  12. [TestFixture]
  13. public class AccountServiceTests : IntegrationBaseTest
  14. {
  15. public UserAuth User { get; set; }
  16.  
  17. [Test]
  18. public void Route_CaseNewDefault_Matches()
  19. {
  20. Host.TestRouteExists(new RouteRegistrationInfo("/role", typeof(Roles), new[] { HttpVerbs.Get }));
  21. Host.TestRouteExists(new RouteRegistrationInfo("/account", typeof(Accounts), new[] { HttpVerbs.Get }));
  22. Host.TestRouteExists(new RouteRegistrationInfo("/account/{Id}", typeof(Account), new[] { HttpVerbs.Get }));
  23. Host.TestRouteExists(new RouteRegistrationInfo("/account/session", typeof(Session), new[] { HttpVerbs.Get }));
  24. Host.TestRouteExists(new RouteRegistrationInfo("/account", typeof(SaveAccount), new[] { HttpVerbs.Post }));
  25. Host.TestRouteExists(new RouteRegistrationInfo("/account/{Id}", typeof(SaveAccount), new[] { HttpVerbs.Put }));
  26. Host.TestRouteExists(new RouteRegistrationInfo("/account/{Id}", typeof(DeleteAccount), new[] { HttpVerbs.Delete }));
  27. }
  28.  
  29. [TearDown]
  30. public void TearDown()
  31. {
  32. ClearDb();
  33. }
  34.  
  35. [SetUp]
  36. public void SetUp()
  37. {
  38. //TODO: remove this and test auth on services
  39. User = Login(Role.SuperAdmin);
  40. }
  41.  
  42. [Test]
  43. public void Roles_ReturnsAllRoles()
  44. {
  45. var response = Client.Get(new Roles());
  46.  
  47. response.Roles.Should().NotBeNull();
  48. response.Roles.Should().HaveCount(5);
  49. Role.AllRoles.ForEach(role => response.Roles.Should().Contain(role));
  50. }
  51.  
  52. [Test]
  53. public void Accounts_SingleAccount_ReturnsOnlyThatAccount()
  54. {
  55. var response = Client.Get(new Accounts());
  56.  
  57. response.Accounts.Should().HaveCount(1);
  58. var user = response.Accounts.Single();
  59. user.Id.Should().Be(User.Id);
  60. user.Email.Should().Be(User.Email);
  61. user.Role.Should().Be(User.Roles.Single());
  62. }
  63.  
  64. [Test]
  65. public void Accounts_MultipleAccounts_ReturnsAllAccounts()
  66. {
  67. var user2 = new UserAuth{ Email = "bla@example.com" };
  68. AuthRepository.CreateUserAuth(user2, "pass");
  69.  
  70. var response = Client.Get(new Accounts());
  71.  
  72. response.Accounts.Should().HaveCount(2);
  73. var accountIds = response.Accounts.Select(x => x.Id);
  74. accountIds.Should().Contain(User.Id);
  75. accountIds.Should().Contain(user2.Id);
  76. }
  77.  
  78. [Test]
  79. public void Account_ReturnsAccount()
  80. {
  81. var response = Client.Get(new Account { Id = User.Id });
  82.  
  83. response.Account.Should().NotBeNull();
  84. response.Account.Id.Should().Be(User.Id);
  85. response.Account.Email.Should().Be(User.Email);
  86. response.Account.Role.Should().Be(User.Roles.Single());
  87. response.Account.FirstName.Should().Be(User.FirstName);
  88. response.Account.LastName.Should().Be(User.LastName);
  89. }
  90.  
  91. [Test]
  92. [ExpectedException(typeof(WebServiceException ), ExpectedMessage = "No account with this id found.")]
  93. public void Account_AccountDoesntExist_404()
  94. {
  95. Client.Get(new Account { Id = -1 });
  96. }
  97.  
  98. //Untestable since real requests are made in a different way?
  99. //[Test]
  100. //public void Session_ReturnsCurrentUserSessionInfo()
  101. //{
  102. // var response = Client.Get(new Session());
  103.  
  104. // response.Session.Should().NotBeNull();
  105. // response.Session.Id.Should().Be(User.Id);
  106. // response.Session.Email.Should().Be(User.Email);
  107. // response.Session.FirstName.Should().Be(User.FirstName);
  108. // response.Session.LastName.Should().Be(User.LastName);
  109. // response.Session.Role.Should().Be(User.Roles.First());
  110. //}
  111.  
  112. [Test]
  113. public void SaveAccount_Post_SavesNewAccount()
  114. {
  115. var request = new SaveAccount {
  116. Email = "foo@bar.com",
  117. FirstName = "foo",
  118. LastName = "bar",
  119. NewPassword = "pass",
  120. NewPasswordConfirm = "pass",
  121. Role = Role.SuperAdmin
  122. };
  123. var response = Client.Post(request);
  124.  
  125. using (var session = Store.OpenSession())
  126. {
  127. var loadedAccount = session.Load<UserAuth>(response.Account.Id);
  128.  
  129. loadedAccount.Should().NotBeNull();
  130. loadedAccount.Email.Should().Be(request.Email);
  131. loadedAccount.FirstName.Should().Be(request.FirstName);
  132. loadedAccount.LastName.Should().Be(request.LastName);
  133. loadedAccount.Roles.Should().Contain(request.Role);
  134. }
  135. }
  136.  
  137. [Test]
  138. public void SaveAccount_Put_UpdatesExistingAccount()
  139. {
  140. var request = new SaveAccount
  141. {
  142. Id = User.Id,
  143. Email = "foo@bar.com",
  144. FirstName = "foo",
  145. LastName = "bar",
  146. NewPassword = "pass",
  147. NewPasswordConfirm = "pass",
  148. Role = Role.SuperAdmin
  149. };
  150. Client.Put(request);
  151.  
  152. using (var session = Store.OpenSession())
  153. {
  154. var loadedAccount = session.Load<UserAuth>(User.Id);
  155.  
  156. loadedAccount.Should().NotBeNull();
  157. loadedAccount.Email.Should().Be(request.Email);
  158. loadedAccount.FirstName.Should().Be(request.FirstName);
  159. loadedAccount.LastName.Should().Be(request.LastName);
  160. loadedAccount.Roles.Should().Contain(request.Role);
  161. }
  162. }
  163.  
  164. [Test]
  165. [ExpectedException(typeof(WebServiceException), ExpectedMessage = "No account with this id found.")]
  166. public void SaveAccount_Put_NoAccountWithThisId_404()
  167. {
  168. var request = new SaveAccount
  169. {
  170. Id = -1,
  171. Email = "foo@bar.com",
  172. FirstName = "foo",
  173. LastName = "bar",
  174. NewPassword = "pass",
  175. NewPasswordConfirm = "pass",
  176. Role = Role.SuperAdmin
  177. };
  178. Client.Put(request);
  179. }
  180.  
  181. [Test]
  182. public void DeleteAccount_DeletesAccount()
  183. {
  184. var request = new DeleteAccount {
  185. Id = User.Id
  186. };
  187. Client.Delete(request);
  188.  
  189. using (var session = Store.OpenSession())
  190. {
  191. var loadedAccount = session.Load<UserAuth>(User.Id);
  192.  
  193. loadedAccount.Should().BeNull();
  194. }
  195. }
  196.  
  197. [Test]
  198. [ExpectedException(typeof(WebServiceException), ExpectedMessage = "No account with this id found.")]
  199. public void DeleteAccount_AccountDoesntExist_404()
  200. {
  201. var request = new DeleteAccount { Id = -1 };
  202. Client.Delete(request);
  203. }
  204. }
  205. }
  206.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty