public static async Task <int> CreateOdataUserAsync(ApiUserDTO userDto, OrganizationRole role, int organizationId = TestEnvironment.DefaultOrganizationId) { var cookie = await GetCookieAsync(OrganizationRole.GlobalAdmin); var createUserDto = ObjectCreateHelper.MakeSimpleCreateUserDto(userDto); int userId; using (var createdResponse = await PostWithCookieAsync(TestEnvironment.CreateUrl("odata/Users/Users.Create"), cookie, createUserDto)) { Assert.Equal(HttpStatusCode.Created, createdResponse.StatusCode); var response = await createdResponse.ReadResponseBodyAsAsync <UserDTO>(); userId = response.Id; Assert.Equal(userDto.Email, response.Email); } using (var addedRole = await SendAssignRoleToUserAsync(userId, role, organizationId)) { Assert.Equal(HttpStatusCode.Created, addedRole.StatusCode); } return(userId); }
public static async Task <Cookie> GetCookieAsync(KitosCredentials userCredentials) { if (CookiesCache.TryGetValue(userCredentials.Username, out var cachedCookie)) { return(cachedCookie); } var url = TestEnvironment.CreateUrl("api/authorize"); var loginDto = ObjectCreateHelper.MakeSimpleLoginDto(userCredentials.Username, userCredentials.Password); var request = CreatePostMessage(url, loginDto); var cookieResponse = await SendWithCSRFToken(request); Assert.Equal(HttpStatusCode.Created, cookieResponse.StatusCode); var cookieParts = cookieResponse.Headers.First(x => x.Key == "Set-Cookie").Value.First().Split('='); var cookieName = cookieParts[0]; var cookieValue = cookieParts[1].Split(';')[0]; var cookie = new Cookie(cookieName, cookieValue) { Domain = url.Host }; CookiesCache.TryAdd(userCredentials.Username, cookie); return(cookie); }
public static async Task <(int userId, KitosCredentials credentials, string token)> CreateUserAndGetToken(string email, OrganizationRole role, int organizationId = TestEnvironment.DefaultOrganizationId, bool apiAccess = false, bool stakeHolderAccess = false) { var userId = await CreateOdataUserAsync(ObjectCreateHelper.MakeSimpleApiUserDto(email, apiAccess, stakeHolderAccess), role, organizationId); var password = Guid.NewGuid().ToString("N"); DatabaseAccess.MutateEntitySet <User>(x => { using var crypto = new CryptoService(); var user = x.AsQueryable().ById(userId); user.Password = crypto.Encrypt(password + user.Salt); }); var token = await GetTokenAsync(new KitosCredentials(email, password)); return(userId, new KitosCredentials(email, password), token.Token); }
private static async Task <GetTokenResponseDTO> GetTokenAsync(KitosCredentials userCredentials) { if (TokenCache.TryGetValue(userCredentials.Username, out var cachedToken)) { return(cachedToken); } var url = TestEnvironment.CreateUrl("api/authorize/GetToken"); var loginDto = ObjectCreateHelper.MakeSimpleLoginDto(userCredentials.Username, userCredentials.Password); using var httpResponseMessage = await PostForKitosToken(url, loginDto); var tokenResponseDtoAsync = await GetTokenResponseDtoAsync(loginDto, httpResponseMessage); TokenCache.TryAdd(userCredentials.Username, tokenResponseDtoAsync); return(tokenResponseDtoAsync); }