Exemplo n.º 1
0
        public void UpdateKey_Fail_NonExistingKeyShouldThrowException()
        {
            // Arrange
            newKey = tu.CreateApiKeyObject();
            var expected = newKey;

            // Act
            using (_db = tu.CreateDataBaseContext())
            {
                IApiKeyService _apiKeyService = new ApiKeyService(_db);

                var response = _apiKeyService.UpdateKey(newKey);
                try
                {
                    _db.SaveChanges();
                }
                catch (System.Data.Entity.Infrastructure.DbUpdateConcurrencyException)
                {
                    _db.Entry(newKey).State = System.Data.Entity.EntityState.Detached;
                }
                catch (System.Data.Entity.Core.EntityCommandExecutionException)
                {
                    _db.Entry(newKey).State = System.Data.Entity.EntityState.Detached;
                }
                var result = _db.Keys.Find(expected.Id);

                // Assert
                Assert.IsNull(response);
                Assert.IsNull(result);
            }
        }
Exemplo n.º 2
0
        public void UpdateKey_Pass_ReturnKey()
        {
            // Arrange
            newKey = tu.CreateApiKeyObject();
            var expected = newKey.Key;

            // Act
            using (_db = tu.CreateDataBaseContext())
            {
                IApiKeyService _apiKeyService = new ApiKeyService(_db);

                newKey = _apiKeyService.CreateKey(newKey);
                _db.SaveChanges();

                newKey.Key = "A new Key";
                var response = _apiKeyService.UpdateKey(newKey);
                _db.SaveChanges();

                var result = _db.Keys.Find(newKey.Id);

                // Assert
                Assert.IsNotNull(response);
                Assert.IsNotNull(result);
                Assert.AreEqual(result.Id, newKey.Id);
                Assert.AreNotEqual(expected, result.Key);

                _apiKeyService.DeleteKey(newKey.Id);
                _db.SaveChanges();
            }
        }
Exemplo n.º 3
0
        public void UpdateKey_Fail_NullValuesShouldReturnNull()
        {
            using (_db = tu.CreateDataBaseContext())
            {
                // Act
                var result = ApiKeyService.UpdateKey(_db, null);

                // Assert
                Assert.IsNull(result);
            }
        }
Exemplo n.º 4
0
        public void UpdateKey_Fail_NullValuesShouldReturnNull()
        {
            bool expected = true;

            using (_db = tu.CreateDataBaseContext())
            {
                IApiKeyService _apiKeyService = new ApiKeyService(_db);

                try
                {
                    // Act
                    var result = _apiKeyService.UpdateKey(null);
                }
                catch (NullReferenceException)
                {
                    expected = false;
                }

                // Assert
                Assert.IsFalse(expected);
            }
        }
Exemplo n.º 5
0
        /// <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);
            }
        }
Exemplo n.º 6
0
        /// <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);
            }
        }