public void GetApiKeyByKey_Fail_NullValuesShouldReturnNull() { using (_db = tu.CreateDataBaseContext()) { // Act var result = ApiKeyService.GetKey(_db, null); // Assert Assert.IsNull(result); } }
public void GetApiKeyByAppIdIsUsed_Fail_NonExistingKeyShouldReturnNull() { // Arrange Guid id = Guid.NewGuid(); ApiKey expected = null; // Act using (_db = tu.CreateDataBaseContext()) { var result = ApiKeyService.GetKey(_db, id, false); // Assert Assert.IsNull(result); Assert.AreEqual(expected, result); } }
public void GetApiKeyByKey_Fail_NonExistingKeyShouldReturnNull() { // Arrange string nonExistingKey = "key"; ApiKey expected = null; // Act using (_db = tu.CreateDataBaseContext()) { var result = ApiKeyService.GetKey(_db, nonExistingKey); // Assert Assert.IsNull(result); Assert.AreEqual(expected, result); } }
public void GetApiKeyById_Fail_NonExistingKeyShouldReturnNull() { // Arrange Guid nonExistingKey = Guid.NewGuid(); ApiKey expected = null; // Act using (_db = tu.CreateDataBaseContext()) { IApiKeyService _apiKeyService = new ApiKeyService(_db); var result = _apiKeyService.GetKey(nonExistingKey); // Assert Assert.IsNull(result); Assert.AreEqual(expected, result); } }
public void GetApiKeyByAppIdIsUsed_Pass_ReturnKey() { // Arrange newKey = tu.CreateApiKeyObject(); var expected = newKey; // Act using (_db = tu.CreateDataBaseContext()) { newKey = ApiKeyService.CreateKey(_db, newKey); _db.SaveChanges(); var result = ApiKeyService.GetKey(_db, newKey.ApplicationId, false); // Assert Assert.IsNotNull(result); Assert.AreEqual(expected.Key, result.Key); ApiKeyService.DeleteKey(_db, newKey.Id); _db.SaveChanges(); } }
public void GetApiKeyByKey_Pass_ReturnKey() { // Arrange newKey = tu.CreateApiKeyObject(); var expected = newKey; // Act using (_db = tu.CreateDataBaseContext()) { IApiKeyService _apiKeyService = new ApiKeyService(_db); newKey = _apiKeyService.CreateKey(newKey); _db.SaveChanges(); var result = _apiKeyService.GetKey(newKey.Key); // Assert Assert.IsNotNull(result); Assert.AreEqual(expected.Key, result.Key); _apiKeyService.DeleteKey(newKey.Id); _db.SaveChanges(); } }
/// <summary> /// Validate the key generation field values, and call key generation services /// </summary> /// <param name="request">Values from POST request</param> /// <returns>Http status code and message</returns> public HttpResponseContent ValidateKeyGeneration(ApplicationRequest request) { // Http status code and message HttpResponseContent response; if (request == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Request."); return(response); } // Validate key generation request values if (request.Title == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Title"); return(response); } else if (request.Email == null || !IsValidEmail(request.Email)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Email"); return(response); } using (var _db = new DatabaseContext()) { // Attempt to find application var app = ApplicationService.GetApplication(_db, request.Title, request.Email); if (app == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Application"); return(response); } // Create a new ApiKey ApiKey apiKey = new ApiKey { Key = _tokenService.GenerateToken(), ApplicationId = app.Id }; // Invalidate old unused api key var keyOld = ApiKeyService.GetKey(_db, app.Id, false); if (keyOld != null) { keyOld.IsUsed = true; keyOld = ApiKeyService.UpdateKey(_db, keyOld); } // Attempt to create an apiKey record var keyResponse = ApiKeyService.CreateKey(_db, apiKey); // Keep generating a new key until a unique one is made. while (keyResponse == null) { apiKey.Key = _tokenService.GenerateToken(); keyResponse = ApiKeyService.CreateKey(_db, apiKey); } List <object> responses = new List <object>(); responses.Add(keyResponse); responses.Add(keyOld); // Save database changes if (!SaveChanges(_db, responses)) { response = new HttpResponseContent(HttpStatusCode.InternalServerError, "Unable to save database changes"); return(response); } string message = apiKey.Key; // TODO: Set up email server to implement email services //string message; //// Attempt to send api key to application email //if (SendNewApiKeyEmail(app.Email, apiKey.Key)) //{ // // Alert front end that email was sent // message = "Sent to " + app.Email; //} //else //{ // // Email could not be sent. Send api key to frontend. // message = apiKey.Key; //} response = new HttpResponseContent(HttpStatusCode.OK, apiKey.Key); return(response); } }
/// <summary> /// Validate the app publish field values, and call publish services /// </summary> /// <param name="request">Values from POST request</param> /// <returns>Http status code and message</returns> public HttpResponseContent ValidatePublish(ApplicationRequest request) { // Http status code and message HttpResponseContent response; if (request == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Request."); return(response); } Uri logoUrl = null; // Validate publish request values if (request.Title == null) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Title."); return(response); } else if (!IsValidStringLength(request.Description, descriptionLength)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Description: Cannot be more than 2000 characters in length."); return(response); } else if (!IsValidUrl(request.LogoUrl, ref logoUrl)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Logo Url."); return(response); } else if (!IsValidImageExtension(logoUrl, imageType)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Logo Image Extension: Can only be .PNG"); return(response); } else if (!IsValidDimensions(logoUrl, xDimension, yDimension)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Logo Dimensions: Can be no more than 55x55 pixels."); return(response); } using (var _db = new DatabaseContext()) { // Attempt to find api key var apiKey = ApiKeyService.GetKey(_db, request.Key); // Key must exist and be unused. if (apiKey == null || apiKey.IsUsed == true) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Key"); return(response); } // Attempt to get application based on ApplicationId from api key var app = ApplicationService.GetApplication(_db, apiKey.ApplicationId); // Published application title is used to authenticate the app. if (app == null || !request.Title.Equals(app.Title)) { response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Key"); return(response); } // Update values of application record app.Description = request.Description; app.LogoUrl = request.LogoUrl; app.UnderMaintenance = request.UnderMaintenance; var appResponse = ApplicationService.UpdateApplication(_db, app); // Update values of api key record apiKey.IsUsed = true; var keyResponse = ApiKeyService.UpdateKey(_db, apiKey); List <object> responses = new List <object>(); responses.Add(appResponse); responses.Add(keyResponse); // Attempt to save database changes if (!SaveChanges(_db, responses)) { // Error response response = new HttpResponseContent(HttpStatusCode.InternalServerError, "Unable to save database changes"); return(response); } // Successful publish response = new HttpResponseContent(HttpStatusCode.OK, "Published to KFC SSO"); return(response); } }