コード例 #1
0
        public void ApiKey_ExecutionRequestValidationShouldFailIfBadToken()
        {
            RegisterDb();
            ServiceProxySystem.Register <ApiKeyRequiredEcho>();

            IUserResolver mockUserResolver = Substitute.For <IUserResolver>();

            mockUserResolver.GetUser(Arg.Any <IHttpContext>()).Returns("testUser");
            LocalApiKeyManager.Default.UserResolver = mockUserResolver;

            string methodName = MethodBase.GetCurrentMethod().Name;
            IApplicationNameProvider nameProvider = new TestApplicationNameProvider(methodName.RandomLetters(4));
            IApiKeyProvider          keyProvider  = new LocalApiKeyProvider();

            ExecutionRequest er = new ExecutionRequest("ApiKeyRequiredEcho", "Send", "json")
            {
                ApiKeyResolver = new ApiKeyResolver(keyProvider, nameProvider),
                Request        = new ServiceProxyTestHelpers.JsonTestRequest()
            };
            string data = ApiParameters.ParametersToJsonParamsObjectString("some random data");

            er.InputString = data;
            ApiKeyResolver resolver = new ApiKeyResolver(keyProvider, nameProvider);

            resolver.SetKeyToken(er.Request.Headers, data);

            er.Request.Headers[Headers.KeyToken] = "bad token value";

            ValidationResult result = er.Validate();

            Expect.IsFalse(result.Success, "Validation should have failed");
            List <ValidationFailures> failures = new List <ValidationFailures>(result.ValidationFailures);

            Expect.IsTrue(failures.Contains(ValidationFailures.InvalidApiKeyToken), "ValidationFailure should have been InvalidApiKeyToken");
        }
コード例 #2
0
        [UnitTest] // RegisterDb is defined in Program.cs
        public void ApiKey_ShouldSetToken()
        {
            ClientTestSetup();
            string testName         = MethodBase.GetCurrentMethod().Name;
            NameValueCollection nvc = new NameValueCollection();
            string data             = "Some random data";

            IApiKeyProvider keyProvider = new LocalApiKeyProvider();
            ApiKeyResolver  resolver    = new ApiKeyResolver(keyProvider, new TestApplicationNameProvider(testName));

            resolver.SetKeyToken(nvc, data);

            Expect.IsNotNullOrEmpty(nvc[Headers.KeyToken], "Key token was not set");
            ClearApps();
        }
コード例 #3
0
        [UnitTest] // RegisterDb is defined in Program.cs
        public void ApiKey_ShouldSetValidToken()
        {
            ClientTestSetup();
            string testName                          = MethodBase.GetCurrentMethod().Name;
            NameValueCollection nvc                  = new NameValueCollection();
            string                      data         = "Some random data";
            IApiKeyProvider             keyProvider  = new LocalApiKeyProvider();
            TestApplicationNameProvider nameProvider = new TestApplicationNameProvider(testName);
            ApiKeyResolver              resolver     = new ApiKeyResolver(keyProvider, nameProvider);

            resolver.SetKeyToken(nvc, data);

            string token   = nvc[Headers.KeyToken];
            bool   isValid = resolver.IsValidKeyToken(data, token);

            Expect.IsTrue(isValid, "token was not valid");
            ClearApps();
        }