public bool UserExists(UserExistsReq userExistsDto) { var user = UnitOfWork.AppUsers.GetByEmail(userExistsDto.EmailId); if (user == null) { return(false); } return(true); }
public void UserExists_ForEmptyEmailId_ReturnsBadRequest() { var restClient = new RestClient(ApiBaseUrl); var objectToPost = new UserExistsReq { EmailId = string.Empty }; RestRequest request = CreateRequestDelayed(ServiceUrl, UserExitsActionUrl, objectToPost); IRestResponse response = restClient.Post(request); string content = response.Content; Assert.IsTrue(response.StatusCode == HttpStatusCode.BadRequest); }
public void Z1_UserExists_For_3RequestsIn1Second_Returns_TooManyRequests() { // Wait for some time to avoid throlling error due to any previous tests DelayBy(3); var restClient = new RestClient(ApiBaseUrl); var objectToPost = new UserExistsReq { EmailId = TestHelper.Constants.DatabaseStaticEntities.AdminUserEmail }; var requests = new List <RestRequest>(); for (int i = 0; i < 3; i++) { RestRequest request = CreateRequest(AccountControllerTests.ServiceUrl, AccountControllerTests.UserExitsActionUrl, objectToPost); requests.Add(request); } var responsesLock = new object(); var responses = new List <IRestResponse>(); var tasks = requests.Select(req => new TaskFactory().StartNew(() => { IRestResponse response = restClient.Post(req); lock (responsesLock) { responses.Add(response); } })); var start = DateTime.UtcNow; Task.WaitAll(tasks.ToArray()); var end = DateTime.UtcNow; var seconds = (end - start).TotalSeconds; Assert.IsTrue(seconds < 1); // Calls must have been made within 1 second only var tooManyRequestsResponse = responses.FirstOrDefault(r => (int)r.StatusCode == 429); // Too many requests Assert.IsNotNull(tooManyRequestsResponse); var apiResponse = JsonConvert.DeserializeObject <ApiResponseDto <object> >(tooManyRequestsResponse.Content); Assert.IsNotNull(apiResponse.Error); // must have error Assert.IsNotNull(apiResponse.Error.Title, "Too many requests"); Assert.IsNotNull(apiResponse.Error.Messages.Any(m => m.Contains("per Second"))); Assert.IsNull(apiResponse.Result); // must not have result }
public void UserExists_ForAdminUser_ReturnsTrue() { var restClient = new RestClient(ApiBaseUrl); var objectToPost = new UserExistsReq { EmailId = TestHelper.Constants.DatabaseStaticEntities.AdminUserEmail }; RestRequest request = CreateRequestDelayed(ServiceUrl, UserExitsActionUrl, objectToPost); IRestResponse response = restClient.Post(request); string jsonContent = response.Content; var apiResponse = JsonConvert.DeserializeObject <ApiResponseDto <bool> >(jsonContent); Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); Assert.IsNull(apiResponse.Error); Assert.IsTrue(apiResponse.Result == true); }
public void UserExists_ForNonExistingUser_ReturnsFalse() { var restClient = new RestClient(ApiBaseUrl); var objectToPost = new UserExistsReq { EmailId = $"abc@{Guid.NewGuid().ToString()}.com" }; RestRequest request = CreateRequestDelayed(ServiceUrl, UserExitsActionUrl, objectToPost); IRestResponse response = restClient.Post(request); string jsonContent = response.Content; var apiResponse = JsonConvert.DeserializeObject <ApiResponseDto <bool> >(jsonContent); Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); Assert.IsNull(apiResponse.Error); Assert.IsTrue(apiResponse.Result == false); }
public void UserExists_ForAdminUser_OverHttp_ReturnsHttpForbidden() { var httpApiUrl = "http" + ApiBaseUrl.TrimStart("https".ToCharArray()); var restClient = new RestClient(httpApiUrl); var objectToPost = new UserExistsReq { EmailId = TestHelper.Constants.DatabaseStaticEntities.AdminUserEmail }; RestRequest request = CreateRequestDelayed(ServiceUrl, UserExitsActionUrl, objectToPost); IRestResponse response = restClient.Post(request); string jsonContent = response.Content; var apiResponse = JsonConvert.DeserializeObject <ApiResponseDto <object> >(jsonContent); Assert.AreEqual(response.StatusCode, HttpStatusCode.Forbidden); Assert.IsNotNull(apiResponse.Error); Assert.IsNull(apiResponse.Result); }
public void Z2_UserExists_For_31RequestsIn1Minute_ReturnsBadRequest() { // Wait for some time to avoid throlling error due to any previous tests DelayBy(3); var restClient = new RestClient(ApiBaseUrl); var objectToPost = new UserExistsReq { EmailId = TestHelper.Constants.DatabaseStaticEntities.AdminUserEmail }; var requests = new List <RestRequest>(); for (int i = 0; i < 31; i++) { RestRequest request = CreateRequest(AccountControllerTests.ServiceUrl, AccountControllerTests.UserExitsActionUrl, objectToPost); requests.Add(request); } var responses = new List <IRestResponse>(); var start = DateTime.UtcNow; foreach (var req in requests) { IRestResponse response = PostDelayed(restClient, req, 1); responses.Add(response); } var end = DateTime.UtcNow; var minutes = (end - start).TotalMinutes; Assert.IsTrue(minutes < 1); // Calls must have been made within 1 minute only var tooManyRequestsResponse = responses.FirstOrDefault(r => (int)r.StatusCode == 429); // Too many requests Assert.IsNotNull(tooManyRequestsResponse); var apiResponse = JsonConvert.DeserializeObject <ApiResponseDto <object> >(tooManyRequestsResponse.Content); Assert.IsNotNull(apiResponse.Error); // must have error Assert.IsNotNull(apiResponse.Error.Title, "Too many requests"); Assert.IsNotNull(apiResponse.Error.Messages.Any(m => m.Contains("per Minute"))); Assert.IsNull(apiResponse.Result); // must not have result }
/// <summary> /// Checks if a user with the specified email id exists on the Api /// </summary> /// <param name="emailId"></param> /// <returns></returns> public static bool CheckIfUserExists(string emailId) { DelayBy((double)ThrottlingConfig.CallsPerSecond); var restClient = new RestClient(ApiBaseUrl); ConsoleOutput($"Created rest client [{restClient.BaseUrl}]"); var objectToPost = new UserExistsReq { EmailId = emailId }; RestRequest request = CreateRequest(AccountControllerTests.ServiceUrl, AccountControllerTests.UserExitsActionUrl, objectToPost); IRestResponse response = restClient.Post(request); ConsoleOutput($"Issued a [{request.Method}] to the resource [{request.Resource}]"); string jsonContent = response.Content; ConsoleOutput($"Got response [{jsonContent}]"); var apiResponse = JsonConvert.DeserializeObject <ApiResponseDto <bool> >(jsonContent); return(apiResponse.Result); }
public IHttpActionResult UserExists(UserExistsReq userExistsDto) { bool userAccountExits = _accountService.UserExists(userExistsDto); return(Ok(userAccountExits)); }