public async Task GenerateApiShouldReturnBadRequestIfOneOfMessagesWasToLong() { const int maxSize = ((AlgorithmConstants.KeySize - 384) / 8 + 37) / 2; using (var httpClient = _factory.CreateClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, "api/password")) { var passwords = new[] { new string(Enumerable.Repeat('s', maxSize).ToArray()), new string(Enumerable.Repeat('s', maxSize + 1).ToArray()) }; var passwordInModel = new PasswordInModel { Passwords = passwords, ExpiresIn = 200 }; request.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); using (var response = await httpClient.SendAsync(request)) { response.StatusCode.ShouldBe(HttpStatusCode.BadRequest); } } }
public async Task PasswordShouldBeRestoredByApiProvidedLink() { using (var httpClient = _factory.CreateClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, "api/password")) { var passwordInModel = new PasswordInModel { Passwords = new[] { "123" }, ExpiresIn = 100 }; request.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); using (var response = await httpClient.SendAsync(request)) { var content = await response.Content.ReadAsAsync <UrlModel>(); using (var retrieveRequest = new HttpRequestMessage(HttpMethod.Get, content.Url)) using (var retrieveResponse = await httpClient.SendAsync(retrieveRequest)) { retrieveResponse.StatusCode.ShouldBe(HttpStatusCode.OK); var passwordOutModel = await retrieveResponse.Content.ReadAsAsync <PasswordOutModel>(); passwordOutModel.Passwords.ShouldBe(passwordInModel.Passwords); } } } }
public async Task GenerateApiShouldAlwaysCreateDifferentKeysEvenForOnePassword() { using (var httpClient = _factory.CreateClient()) using (var request1 = new HttpRequestMessage(HttpMethod.Post, "api/password")) using (var request2 = new HttpRequestMessage(HttpMethod.Post, "api/password")) { var passwordInModel = new PasswordInModel { Passwords = new[] { "123" }, ExpiresIn = 200 }; request1.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); request2.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); using (var response1 = await httpClient.SendAsync(request1)) using (var response2 = await httpClient.SendAsync(request2)) { var content1 = await response1.Content.ReadAsAsync <UrlModel>(); var uri1 = new Uri(content1.Url); var key1 = HttpUtility.ParseQueryString(uri1.Query).Get("key"); var content2 = await response2.Content.ReadAsAsync <UrlModel>(); var uri2 = new Uri(content2.Url); var key2 = HttpUtility.ParseQueryString(uri2.Query).Get("key"); key1.ShouldNotBe(key2); } } }
public async Task ApiShouldReturnBadRequestIfLinkReused() { using (var httpClient = _factory.CreateClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, "api/password")) { var passwordInModel = new PasswordInModel { Passwords = new[] { "123" }, ExpiresIn = 100 }; request.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); using (var response = await httpClient.SendAsync(request)) { var content = await response.Content.ReadAsAsync <UrlModel>(); using (var retrieveRequest1 = new HttpRequestMessage(HttpMethod.Get, content.Url)) using (var retrieveRequest2 = new HttpRequestMessage(HttpMethod.Get, content.Url)) using (var retrieveResponse1 = await httpClient.SendAsync(retrieveRequest1)) using (var retrieveResponse2 = await httpClient.SendAsync(retrieveRequest2)) { retrieveResponse1.StatusCode.ShouldBe(HttpStatusCode.OK); retrieveResponse2.StatusCode.ShouldBe(HttpStatusCode.BadRequest); } } } }
public async Task ApiShouldReturnBadRequestIfPasswordNotExists() { using (var httpClient = _factory.CreateClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, "api/password")) { var passwordInModel = new PasswordInModel { Passwords = new[] { "123" }, ExpiresIn = 200 }; request.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); using (var response = await httpClient.SendAsync(request)) { var content = await response.Content.ReadAsAsync <UrlModel>(); var uri = new Uri(content.Url); var key = HttpUtility.ParseQueryString(uri.Query).Get("key"); using (var retrieveRequest = new HttpRequestMessage(HttpMethod.Get, $"api/password/0?key={key}")) using (var retrieveResponse = await httpClient.SendAsync(retrieveRequest)) { retrieveResponse.StatusCode.ShouldBe(HttpStatusCode.BadRequest); } } } }
public async Task ApiShouldReturnBadRequestIfPasswordStatusChanged() { using (var httpClient = _factory.CreateClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, "api/password")) { var passwordInModel = new PasswordInModel { Passwords = new[] { "123" }, ExpiresIn = 2 }; request.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); using (var response = await httpClient.SendAsync(request)) { var content = await response.Content.ReadAsAsync <UrlModel>(); await Task.Delay(TimeSpan.FromSeconds(3)); using (var retrieveRequest = new HttpRequestMessage(HttpMethod.Get, content.Url)) using (var retrieveResponse = await httpClient.SendAsync(retrieveRequest)) { retrieveResponse.StatusCode.ShouldBe(HttpStatusCode.BadRequest); } } } }
public async Task <IActionResult> Generate([FromBody, BindRequired] PasswordInModel inModel) { var request = new GeneratePasswordLinkRequest(inModel.Passwords, inModel.ExpiresIn); var result = await _mediator.Send(request); var urlRequest = new RouteLinkRequest(Url, result.PasswordGroupId, result.Key); var url = await _mediator.Send(urlRequest); return(Ok(new UrlModel { Url = url })); }
public async Task ParallelRetrieveTest() { const int load = 100; using (var httpClient = _factory.CreateClient()) { for (var i = 0; i < load; i++) { using (var request = new HttpRequestMessage(HttpMethod.Post, "api/password")) { var passwordInModel = new PasswordInModel { Passwords = new[] { "123" }, ExpiresIn = 100 }; request.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); using (var response = await httpClient.SendAsync(request)) { var content = await response.Content.ReadAsAsync <UrlModel>(); using (var retrieveRequest1 = new HttpRequestMessage(HttpMethod.Get, content.Url)) using (var retrieveRequest2 = new HttpRequestMessage(HttpMethod.Get, content.Url)) using (var retrieveResponse1Task = httpClient.SendAsync(retrieveRequest1)) using (var retrieveResponse2Task = httpClient.SendAsync(retrieveRequest2)) { await Task.WhenAll(retrieveResponse1Task, retrieveResponse2Task); if (retrieveResponse1Task.Result.StatusCode == HttpStatusCode.OK) { retrieveResponse1Task.Result.StatusCode.ShouldBe(HttpStatusCode.OK); retrieveResponse2Task.Result.StatusCode.ShouldBe(HttpStatusCode.BadRequest); } else { retrieveResponse2Task.Result.StatusCode.ShouldBe(HttpStatusCode.OK); retrieveResponse1Task.Result.StatusCode.ShouldBe(HttpStatusCode.BadRequest); } } } } } } }
public async Task LoadApiTest() { const int load = 100; using (var httpClient = _factory.CreateClient()) { var allTasks = new List <Task>(); for (var i = 0; i < load; i++) { var task = Task.Run(async() => { using (var request = new HttpRequestMessage(HttpMethod.Post, "api/password")) { var passwordInModel = new PasswordInModel { Passwords = new[] { "1", "2", "3" }, ExpiresIn = 100 }; request.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); using (var response = await httpClient.SendAsync(request)) { var content = await response.Content.ReadAsAsync <UrlModel>(); using (var retrieveRequest = new HttpRequestMessage(HttpMethod.Get, content.Url)) using (var retrieveResponse = await httpClient.SendAsync(retrieveRequest)) { retrieveResponse.StatusCode.ShouldBe(HttpStatusCode.OK); var passwordOutModel = await retrieveResponse.Content.ReadAsAsync <PasswordOutModel>(); passwordOutModel.Passwords.ShouldBe(passwordInModel.Passwords); } } } }); allTasks.Add(task); } await Task.WhenAll(allTasks); } }
public async Task GenerateShouldGenerateLinkLessThan2048Symbols() { using (var httpClient = _factory.CreateClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, "api/password")) { var passwordInModel = new PasswordInModel { Passwords = new[] { "123" }, ExpiresIn = 100 }; request.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); using (var response = await httpClient.SendAsync(request)) { response.StatusCode.ShouldBe(HttpStatusCode.OK); var content = await response.Content.ReadAsAsync <UrlModel>(); content.Url.Length.ShouldBeLessThan(2048); } } }
public async Task GenerateShouldGenerateLinkToPassword() { using (var httpClient = _factory.CreateClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, "api/password")) { var passwordInModel = new PasswordInModel { Passwords = new[] { "123" }, ExpiresIn = 100 }; request.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); using (var response = await httpClient.SendAsync(request)) { response.StatusCode.ShouldBe(HttpStatusCode.OK); var content = await response.Content.ReadAsAsync <UrlModel>(); content.Url.ShouldStartWith("http://localhost/api/password", Case.Insensitive); } } }
public async Task ApiShouldReturnBadRequestIfWrongKeyProvided() { using (var httpClient = _factory.CreateClient()) using (var request1 = new HttpRequestMessage(HttpMethod.Post, "api/password")) using (var request2 = new HttpRequestMessage(HttpMethod.Post, "api/password")) { var passwordInModel = new PasswordInModel { Passwords = new[] { "123" }, ExpiresIn = 200 }; request1.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); request2.Content = new ObjectContent <PasswordInModel>(passwordInModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json); using (var response1 = await httpClient.SendAsync(request1)) using (var response2 = await httpClient.SendAsync(request2)) { var content1 = await response1.Content.ReadAsAsync <UrlModel>(); var uri1 = new Uri(content1.Url); var key1 = HttpUtility.ParseQueryString(uri1.Query).Get("key"); var content2 = await response2.Content.ReadAsAsync <UrlModel>(); var uri2 = new Uri(content2.Url); var query2 = HttpUtility.ParseQueryString(uri1.Query); query2.Set("key", key1); using (var retrieveRequest = new HttpRequestMessage(HttpMethod.Get, $"{uri2.AbsolutePath}?{query2}")) using (var retrieveResponse = await httpClient.SendAsync(retrieveRequest)) { retrieveResponse.StatusCode.ShouldBe(HttpStatusCode.BadRequest); } } } }