Exemple #1
0
        public void CreateKey_Fail_ExistingKeyShouldReturnNull()
        {
            // Arrange
            newKey = tu.CreateApiKeyObject();
            var expected = newKey;

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

                // Act
                var response = _apiKeyService.CreateKey(newKey);
                _db.SaveChanges();

                var actual = _apiKeyService.CreateKey(newKey);

                // Assert
                Assert.AreNotEqual(expected, actual);
                Assert.IsNotNull(response);
                Assert.IsNull(actual);

                _applicationService.DeleteApplication(newKey.ApplicationId);
                _db.SaveChanges();
            }
        }
Exemple #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();
            }
        }
Exemple #3
0
        public void CreateKey_Fail_NullValuesShouldReturnNull()
        {
            using (_db = tu.CreateDataBaseContext())
            {
                // Act
                var result = ApiKeyService.CreateKey(_db, null);

                // Assert
                Assert.IsNull(result);
            }
        }
Exemple #4
0
        public void CreateKey_Pass_ReturnKey()
        {
            // Arrange
            newKey = tu.CreateApiKeyObject();
            var expected = newKey;

            using (_db = tu.CreateDataBaseContext())
            {
                // Act
                var response = ApiKeyService.CreateKey(_db, newKey);
                _db.SaveChanges();

                // Assert
                Assert.IsNotNull(response);
                Assert.AreEqual(response.Id, expected.Id);

                ApiKeyService.DeleteKey(_db, newKey.Id);
                _db.SaveChanges();
            }
        }
Exemple #5
0
        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();
            }
        }
Exemple #6
0
        public void CreateKey_Fail_NullValuesShouldReturnNullReferenceException()
        {
            bool expected = true;

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

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

                // Assert
                Assert.IsFalse(expected);
            }
        }
Exemple #7
0
        public void CreateKey_Fail_ExistingKeyShouldReturnNull()
        {
            // Arrange
            newKey = tu.CreateApiKeyObject();
            var expected = newKey;

            using (_db = tu.CreateDataBaseContext())
            {
                // Act
                var response = ApiKeyService.CreateKey(_db, newKey);
                _db.SaveChanges();

                var actual = ApiKeyService.CreateKey(_db, newKey);

                // Assert
                Assert.IsNull(actual);
                Assert.AreNotEqual(expected, actual);

                ApiKeyService.DeleteKey(_db, newKey.Id);
                _db.SaveChanges();
            }
        }
Exemple #8
0
        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();
            }
        }
Exemple #9
0
        /// <summary>
        /// Validate the app registration field values, and call registration services
        /// </summary>
        /// <param name="request">Values from POST request</param>
        /// <returns>Http status code and message</returns>
        public HttpResponseContent ValidateRegistration(ApplicationRequest request)
        {
            // Http status code and message
            HttpResponseContent response;

            if (request == null)
            {
                response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Request.");
                return(response);
            }

            Uri launchUrl = null;
            Uri deleteUrl = null;

            // Validate request values
            if (request.Title == null || !IsValidStringLength(request.Title, titleLength))
            {
                response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Title: Cannot be more than 100 characters in length.");
                return(response);
            }
            else if (request.Email == null || !IsValidEmail(request.Email))
            {
                response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Email");
                return(response);
            }
            else if (request.LaunchUrl == null || !IsValidUrl(request.LaunchUrl, ref launchUrl) || !IsValidStringLength(request.LaunchUrl, urlLength))
            {
                response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid Application Url");
                return(response);
            }
            else if (request.DeleteUrl == null || !IsValidUrl(request.DeleteUrl, ref deleteUrl) || !IsValidStringLength(request.DeleteUrl, urlLength))
            {
                response = new HttpResponseContent(HttpStatusCode.BadRequest, "Invalid User Deletion Url");
                return(response);
            }

            // Create a new application
            Application app = new Application
            {
                Title           = request.Title,
                LaunchUrl       = launchUrl.ToString(),
                Email           = request.Email,
                UserDeletionUrl = request.DeleteUrl,
                SharedSecretKey = _tokenService.GenerateToken()
            };

            // Create a new ApiKey
            ApiKey apiKey = new ApiKey
            {
                // Generate a unique key
                Key           = _tokenService.GenerateToken(),
                ApplicationId = app.Id
            };

            using (var _db = new DatabaseContext())
            {
                // Attempt to create an Application record
                var appResponse = ApplicationService.CreateApplication(_db, app);
                if (appResponse == null)
                {
                    response = new HttpResponseContent(HttpStatusCode.BadRequest, "Application Already Exists");
                    return(response);
                }

                // 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(appResponse);
                responses.Add(keyResponse);

                // Save database changes
                if (!SaveChanges(_db, responses))
                {
                    response = new HttpResponseContent(HttpStatusCode.InternalServerError, "Unable to save database changes");
                    return(response);
                }
            }

            // Attempt to send api key to application email
            //if (SendAppRegistrationApiKeyEmail(app.Email, apiKey.Key))
            //{
            //    // Alert front end that email was sent
            //    string message = "Sent to " + app.Email;
            //    response = new HttpResponseContent(HttpStatusCode.OK, message);
            //}
            //else
            //{
            //    // Email could not be sent. Send api key to frontend.
            //    response = new HttpResponseContent(HttpStatusCode.OK, apiKey.Key, app.SharedSecretKey);
            //}


            // Return success messge
            response = new HttpResponseContent(HttpStatusCode.OK, apiKey.Key, app.SharedSecretKey);
            return(response);
        }
Exemple #10
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);
            }
        }