public async Task <IActionResult> Register([FromBody] RegisterViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new ManagedUser { UserName = model.UserName, FirstName = model.FirstName, LastName = model.LastName, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); string role = "Basic User"; if (result.Succeeded) { await _userManager.AddToRoleAsync(user, role); await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim("userName", user.UserName)); await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim("firstName", user.FirstName)); await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim("lastName", user.LastName)); await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim("email", user.Email)); await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim("role", role)); return(Ok(new ProfileViewModel(user))); } return(BadRequest(result.Errors)); }
public void TestCreateOrUpdateUserThrows() { ManagedUser u = null; try { // Arrange var ps = new PlatformService(); // Act Action f1 = () => { u = (ManagedUser)ps.CreateOrUpdateUser(null, null, null); }; // Assert f1.ShouldThrow <ArgumentException>().WithMessage("User information was invalid."); Action f2 = () => { u = (ManagedUser)ps.CreateOrUpdateUser(null, null, new RemoteUserInfo()); }; f2.ShouldThrow <ArgumentException>().WithMessage("Value cannot be null.\r\nParameter name: id"); Action f3 = () => { u = (ManagedUser)ps.CreateOrUpdateUser(TestPlatformId, null, new RemoteUserInfo()); }; f3.ShouldNotThrow(); } finally { if (u != null) { Entity.Delete(u); } } }
public void TestDeleteUser() { ManagedPlatform p = null; ManagedTenant t = null; ManagedUser u = null; try { // Arrange var ps = new PlatformService(); p = CreateTestPlatform(); t = CreateTestTenant(p); u = CreateTestUser(t); p.Save(); p.Should().NotBeNull(); t.Should().NotBeNull(); u.Should().NotBeNull(); var pid = p.Id; var tid = t.Id; var uid = u.Id; // Act ps.DeleteUser(TestPlatformId, TestTenantName, TestUserName); // Assert var e1 = Entity.Get(pid); e1.Should().NotBeNull(); var e2 = Entity.Get(tid); e2.Should().NotBeNull(); var e3 = Entity.Get(uid); e3.Should().BeNull(); } finally { if (u != null) { Entity.Delete(u); } if (t != null) { Entity.Delete(t); } if (p != null) { Entity.Delete(p); } } }
public void TestCreateOrUpdateUser() { ManagedPlatform p = null; ManagedTenant t = null; ManagedUser u = null; try { // Arrange var ps = new PlatformService(); var ui = new RemoteUserInfo { Name = TestUserName }; // Act u = (ManagedUser)ps.CreateOrUpdateUser(TestPlatformId, TestTenantName, ui); // Assert u.Should().BeNull(); p = CreateTestPlatform(); t = CreateTestTenant(p); p.Save(); u = (ManagedUser)ps.CreateOrUpdateUser(TestPlatformId, TestTenantName, ui); u.Should().BeNull(); ui.RemoteId = TestUserRemoteId; u = (ManagedUser)ps.CreateOrUpdateUser(TestPlatformId, TestTenantName, ui); u.Should().NotBeNull(); u.Name.Should().Be(TestUserName); u.RemoteId.Should().Be(TestUserRemoteId.ToString()); u.Status_Enum.Should().Be(ManagedUserStatusEnumeration.Unknown); u.Tenant.Name.Should().Be(TestTenantName); u.Tenant.Platform.DatabaseId.Should().Be(TestPlatformId); var uid = u.Id; u = (ManagedUser)ps.CreateOrUpdateUser(TestPlatformId, TestTenantName, new RemoteUserInfo { RemoteId = TestUserRemoteId, Name = "Another Name", Status = UserStatus.Expired }); u.Should().NotBeNull(); u.Id.Should().Be(uid); u.Name.Should().Be("Another Name"); u.RemoteId.Should().Be(TestUserRemoteId.ToString()); u.Status_Enum.Should().Be(ManagedUserStatusEnumeration.Expired); u.Tenant.Name.Should().Be(TestTenantName); u.Tenant.Platform.DatabaseId.Should().Be(TestPlatformId); } finally { if (u != null) { Entity.Delete(u); } if (t != null) { Entity.Delete(t); } if (p != null) { Entity.Delete(p); } } }
public ProfileViewModel(ManagedUser user) { Id = user.Id; }