Ejemplo n.º 1
0
        public async Task ResourcesToAuthenticateAccess_Received()
        {
            // Arrange:
            var mockMessaging = MockMessagingService.Setup(results => {
                var expectedResult = AuthResult.Authenticated().SetSignedToken("MOCK_TOKEN");
                results.RegisterResponse <AuthenticateCaller, AuthResult>(expectedResult);
            });

            var expectedService = "resource-owner";
            var scope1          = "repository:test/my-app:pull,push";
            var scope2          = "repository:test/my-app2:pull";

            var url = $@"api/boondocks/authentication?service={expectedService}&scope={scope1}&scope={scope2}";

            var plugin     = new MockAppHostPlugin();
            var httpClient = TestHttpClient.Create(plugin, mockMessaging);

            // Act:
            var credentials = new AuthCredentialModel {
            };
            var result      = await httpClient.AuthenticateAsync(url, credentials);

            // Assert:
            Assert.True(mockMessaging.ReceivedMessages.Count() == 1);

            var receivedCommand = (AuthenticateCaller)mockMessaging.ReceivedMessages.First();

            Assert.NotNull(receivedCommand.Context);

            // Assert Owning Service Submitted:
            Assert.Equal(receivedCommand.Context.ResourceOwner, expectedService);

            // Assert Resources to Authenticate Access:
            Assert.NotNull(receivedCommand.Context.Resources);
            Assert.True(receivedCommand.Context.Resources.Length == 2);

            // Assert First Resource Scope:
            var firstScope = receivedCommand.Context.Resources[0];

            Assert.Equal("repository", firstScope.Type);
            Assert.Equal("test/my-app", firstScope.Name);
            Assert.True(firstScope.Actions.Length == 2);
            Assert.Equal("pull", firstScope.Actions[0]);
            Assert.Equal("push", firstScope.Actions[1]);


            // Assert Second Resource Scope:
            var secondScope = receivedCommand.Context.Resources[1];

            Assert.Equal("repository", secondScope.Type);
            Assert.Equal("test/my-app2", secondScope.Name);
            Assert.True(secondScope.Actions.Length == 1);
            Assert.Equal("pull", firstScope.Actions[0]);
        }
Ejemplo n.º 2
0
        public async Task InvalidSpecifiedCredentials_UnAuthorized()
        {
            // Arrange:
            var mockMessaging = MockMessagingService.Setup(results => {
                var expectedResult = AuthResult.SetAuthenticated(false);
                results.RegisterResponse <AuthenticateCaller, AuthResult>(expectedResult);
            });

            var plugin     = new MockAppHostPlugin();
            var httpClient = TestHttpClient.Create(plugin, mockMessaging);

            // Act:
            var credentials = new AuthCredentialModel {
            };
            var result      = await httpClient.AuthenticateAsync(credentials);

            // Assert:
            Assert.Equal(HttpStatusCode.Unauthorized, result.StatusCode);
        }
Ejemplo n.º 3
0
        public async Task ClientCredentialsReceived_WhenSent()
        {
            // Arrange:
            var mockMessaging = MockMessagingService.Setup(results => {
                var expectedResult = AuthResult.Authenticated().SetSignedToken("MOCK_TOKEN");
                results.RegisterResponse <AuthenticateCaller, AuthResult>(expectedResult);
            });

            var plugin     = new MockAppHostPlugin();
            var httpClient = TestHttpClient.Create(plugin, mockMessaging);

            // Act:
            var credentialModle = new AuthCredentialModel
            {
                Credentials = new Dictionary <string, string>
                {
                    { "CertKey1", "CertValue1" },
                    { "CertKey2", "CertValue2" }
                }
            };

            var result = await httpClient.AuthenticateAsync(credentialModle);

            // Assert:
            Assert.True(mockMessaging.ReceivedMessages.Count() == 1);

            var receivedCommand = (AuthenticateCaller)mockMessaging.ReceivedMessages.First();

            Assert.NotNull(receivedCommand.Context);

            var receivedCredentials = receivedCommand.Context.Credentials;

            Assert.NotNull(receivedCredentials);

            Assert.True(receivedCredentials.ContainsKey("CertKey1"));
            Assert.True(receivedCredentials.ContainsKey("CertKey2"));
            Assert.Equal("CertValue1", receivedCredentials["CertKey1"]);
            Assert.Equal("CertValue2", receivedCredentials["CertKey2"]);
        }
Ejemplo n.º 4
0
        public async Task ValidCredentials_ResourceAccess_Returned()
        {
            // Arrange:
            var allowedResourceAccess = new ResourcePermission[] {
                new ResourcePermission(type: "ResourceType", name: "ResourceName", actions: new string[] { "action1", "action2" })
            };

            var mockMessaging = MockMessagingService.Setup(results => {
                var expectedResult = AuthResult.Authenticated(allowedResourceAccess)
                                     .SetSignedToken("MOCK_TOKEN");
                results.RegisterResponse <AuthenticateCaller, AuthResult>(expectedResult);
            });

            var plugin     = new MockAppHostPlugin();
            var httpClient = TestHttpClient.Create(plugin, mockMessaging);

            // Act:
            var credentials = new AuthCredentialModel {
            };
            var result      = await httpClient.AuthenticateAsync(credentials);

            // Assert:
            var responseValue = await result.Content.ReadAsStringAsync();

            var resource = JsonConvert.DeserializeObject <AuthResultResource>(responseValue);
            var resourcesGrantedAccess = resource.GetEmbeddedCollection <AuthAccessResource>("resource-access");

            Assert.NotNull(resourcesGrantedAccess);
            Assert.True(resourcesGrantedAccess.Count() == 1);

            var access = resourcesGrantedAccess.First();

            Assert.Equal("ResourceType", access.Type);
            Assert.Equal("ResourceName", access.Name);
            Assert.True(access.Actions.Length == 2);
            Assert.Equal("action1", access.Actions[0]);
            Assert.Equal("action2", access.Actions[1]);
        }
Ejemplo n.º 5
0
        public async Task ValidCredentials_OkStatus_WithTokenHeader()
        {
            // Arrange:
            var mockMessaging = MockMessagingService.Setup(results => {
                var expectedResult = AuthResult.Authenticated()
                                     .SetSignedToken("MOCK_TOKEN");
                results.RegisterResponse <AuthenticateCaller, AuthResult>(expectedResult);
            });

            var plugin     = new MockAppHostPlugin();
            var httpClient = TestHttpClient.Create(plugin, mockMessaging);

            // Act:
            var credentials = new AuthCredentialModel {
            };
            var result      = await httpClient.AuthenticateAsync(credentials);

            // Assert:
            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
            result.Headers.TryGetValues("X-Custom-Token", out IEnumerable <string> values);
            Assert.True(values.Count() == 1);
            Assert.Equal("MOCK_TOKEN", values.First());
        }
Ejemplo n.º 6
0
        public async Task IncorrectedOrMissingCredentials_BadRequest()
        {
            // Arrange:
            var mockMessaging = MockMessagingService.Setup(results => {
                var expectedResult = AuthResult.Failed("INVALID_CONTEXT");
                results.RegisterResponse <AuthenticateCaller, AuthResult>(expectedResult);
            });

            var plugin     = new MockAppHostPlugin();
            var httpClient = TestHttpClient.Create(plugin, mockMessaging);

            // Act:
            var credentials = new AuthCredentialModel {
            };
            var result      = await httpClient.AuthenticateAsync(credentials);

            // Assert:
            Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode);
            Assert.NotNull(result.Content);

            var content = await result.Content.ReadAsStringAsync();

            Assert.Equal("INVALID_CONTEXT", content);
        }