public void Get_ExistingValidApikey_ResultsInOK()
        {
            var apikey = new Model.CustomerApiKey
            {
                Id      = 1,
                ApiKey  = "XXX",
                Comment = "comment1",

                ChangedBy   = "Name1",
                Changed     = DateTime.Now,
                CreatedBy   = "Name1",
                Created     = DateTime.Now,
                Expires     = DateTime.Now.AddYears(1),
                IsPublic    = true,
                Locked      = false,
                LockedOn    = null,
                LockedUntil = null,
            };
            var mock = CreateMock();

            mock.Setup(x => x.GetCustomerApiKeyById(1)).Returns(apikey);
            var controller = new CustomerApiKeyController(() => mock.Object)
            {
                Request       = new HttpRequestMessage(),
                Configuration = new HttpConfiguration()
            };
            var ret = controller.Get(1);

            Assert.AreEqual(Newtonsoft.Json.JsonConvert.SerializeObject(apikey), ret.Content.ReadAsStringAsync().Result);
        }
        public void Put_ValidObjectPassed_ReturnsIsOk()
        {
            var existingObj = new Model.CustomerApiKey
            {
                ApiKey  = "XXX",
                Comment = "comment1",

                ChangedBy   = "Name1",
                Changed     = DateTime.Now,
                CreatedBy   = "Name1",
                Created     = DateTime.Now,
                Expires     = DateTime.Now.AddYears(1),
                IsPublic    = true,
                Locked      = false,
                LockedOn    = null,
                LockedUntil = null,
            };

            var mock = CreateMock();

            mock.Setup(x => x.UpdateCustomerApiKey(existingObj)).Returns(1);
            var controller = new CustomerApiKeyController(() => mock.Object)
            {
                Request       = new HttpRequestMessage(),
                Configuration = new HttpConfiguration()
            };

            var updatedObj = new Model.CustomerApiKey {
                ApiKey = "XXX", Comment = "comment2", ChangedBy = "Name2", IsPublic = false
            };
            var ret = controller.Put(updatedObj);

            Assert.AreEqual(Newtonsoft.Json.JsonConvert.SerializeObject(existingObj.ApiKey), ret.Content.ReadAsStringAsync().Result);
            Assert.AreEqual(200, (int)ret.StatusCode);
        }
        public void Post_InvalidObjectPassed_ReturnsBadRequest()
        {
            var apikeyMissingModel = new Model.CustomerApiKey
            {
                Comment = "comment1",

                ChangedBy   = "Name1",
                Changed     = DateTime.Now,
                CreatedBy   = "Name1",
                Created     = DateTime.Now,
                Expires     = DateTime.Now.AddYears(1),
                IsPublic    = true,
                Locked      = false,
                LockedOn    = null,
                LockedUntil = null,
            };

            var mock       = CreateMock();
            var controller = new CustomerApiKeyController(() => mock.Object)
            {
                Request       = new HttpRequestMessage(),
                Configuration = new HttpConfiguration()
            };

            controller.ModelState.AddModelError("Apikey", "Required");
            var badResponse = controller.Post(apikeyMissingModel);

            Assert.AreEqual(500, (int)badResponse.StatusCode);
        }
        public void Post_ValidObjectPassed_ReturnsIsOk()
        {
            var mock       = CreateMock();
            var controller = new CustomerApiKeyController(() => mock.Object)
            {
                Request       = new HttpRequestMessage(),
                Configuration = new HttpConfiguration()
            };

            var customerApikeyTestModel = new Model.CustomerApiKey
            {
                ApiKey  = "XXX",
                Comment = "comment1",

                ChangedBy   = "Name1",
                Changed     = DateTime.Now,
                CreatedBy   = "Name1",
                Created     = DateTime.Now,
                Expires     = DateTime.Now.AddYears(1),
                IsPublic    = true,
                Locked      = false,
                LockedOn    = null,
                LockedUntil = null,
            };

            var result = controller.Post(customerApikeyTestModel);

            Assert.IsNotNull(result);
            Assert.AreEqual(200, (int)result.StatusCode);
        }
        public Model.CustomerApiKey GetCustomerApiKeyById(int id)
        {
            CustomerApiKey customerApiKey = _context.CustomerApiKey.SingleOrDefault(x => id.Equals(x.Id));

            Model.CustomerApiKey customerApiKeyModel = customerApiKey?.MapFull();

            return(customerApiKeyModel);
        }
        public int AddCustomerApiKey(Model.CustomerApiKey customerApiKeyModel)
        {
            if (string.IsNullOrEmpty(customerApiKeyModel.ApiKey))
            {
                throw new ArgumentException($"Apikey value is null or empty");
            }

            CustomerApiKey customerApiKey = customerApiKeyModel.MapFull();

            _context.CustomerApiKey.Add(customerApiKey);
            _context.Save();

            return(customerApiKey.Id);
        }
Пример #7
0
        public static CustomerApiKey MapFull(this ApiKeyModel.CustomerApiKey customerApiKeyModel, CustomerApiKey customerApiKey = null)
        {
            var tmp = customerApiKey ?? new CustomerApiKey();

            tmp.ApiKey    = customerApiKeyModel.ApiKey;
            tmp.Comment   = customerApiKeyModel.Comment;
            tmp.Expires   = customerApiKeyModel.Expires;
            tmp.IsPublic  = customerApiKeyModel.IsPublic;
            tmp.Created   = customerApiKeyModel.Created == DateTime.MinValue ? tmp.Created : customerApiKeyModel.Created;
            tmp.CreatedBy = customerApiKeyModel.CreatedBy ?? tmp.CreatedBy;
            tmp.Changed   = customerApiKeyModel.Changed;
            tmp.ChangedBy = customerApiKeyModel.ChangedBy;

            return(tmp);
        }
        public int UpdateCustomerApiKey(Model.CustomerApiKey customerApiKeyModel)
        {
            if (customerApiKeyModel.Id == 0)
            {
                throw new ArgumentException($"Id is not valid");
            }

            CustomerApiKey customerApiKey = _context.CustomerApiKey.SingleOrDefault(x => x.Id.Equals(customerApiKeyModel.Id));

            if (customerApiKey == null)
            {
                throw new ArgumentException($"No data found with id: {customerApiKeyModel.Id}");
            }

            customerApiKeyModel.MapFull(customerApiKey);

            _context.Save();
            return(customerApiKey.Id);
        }
Пример #9
0
        public HttpResponseMessage Post(Model.CustomerApiKey customerApiKeyModel)
        {
            try
            {
                if (customerApiKeyModel == null)
                {
                    throw new HttpRequestException("customer apikey model is null");
                }
                if (!ModelState.IsValid)
                {
                    throw new HttpRequestException("model is invalid");
                }

                return(Request.CreateResponse(HttpStatusCode.OK, _customerApiKeyFacade().AddCustomerApiKey(customerApiKeyModel)));
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message));
            }
        }
Пример #10
0
        public static ApiKeyModel.CustomerApiKey MapFull(this CustomerApiKey customerApiKey)
        {
            ApiKeyModel.CustomerApiKey customerApiKeyModel = new ApiKeyModel.CustomerApiKey
            {
                Id                  = customerApiKey.Id,
                ApiKey              = customerApiKey.ApiKey,
                Comment             = customerApiKey.Comment,
                Expires             = customerApiKey.Expires,
                IsPublic            = customerApiKey.IsPublic,
                Locked              = customerApiKey.Locked,
                LockedOn            = customerApiKey.LockedOn,
                LockedUntil         = customerApiKey.LockedUntil,
                Created             = customerApiKey.Created,
                CreatedBy           = customerApiKey.CreatedBy,
                Changed             = customerApiKey.Changed,
                ChangedBy           = customerApiKey.ChangedBy,
                CustomerApiKeyRules = customerApiKey.CustomerApiKeyRules.MapFull(customerApiKey.Id),
            };

            return(customerApiKeyModel);
        }