Exemplo n.º 1
0
        public async Task TestListTenants()
        {
            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource())
            {
                cancellationTokenSource.CancelAfter(TestTimeout(TimeSpan.FromSeconds(10)));

                TestCredentials credentials = Credentials;
                Assert.IsNotNull(credentials);

                AuthenticationRequest request = credentials.AuthenticationRequest;
                Assert.IsNotNull(request);

                IdentityV2AuthenticationService authenticationService = new IdentityV2AuthenticationService(CreateService(), request);

                using (IIdentityService service = CreateService(authenticationService))
                {
                    ListTenantsApiCall apiCall = await service.PrepareListTenantsAsync(cancellationTokenSource.Token);

                    Tuple <HttpResponseMessage, ReadOnlyCollectionPage <Tenant> > response = await apiCall.SendAsync(cancellationTokenSource.Token);

                    Assert.IsNotNull(response);
                    Assert.IsNotNull(response.Item2);

                    ReadOnlyCollectionPage <Tenant> tenants = response.Item2;
                    Assert.IsNotNull(tenants);
                    Assert.AreNotEqual(0, tenants.Count);
                    Assert.IsFalse(tenants.CanHaveNextPage);

                    foreach (Tenant tenant in tenants)
                    {
                        CheckTenant(tenant);
                    }

                    Assert.IsTrue(tenants.Any(i => i.Enabled ?? false));
                }
            }
        }
        internal static IAuthenticationService CreateAuthenticationService(TestCredentials credentials, bool logRequests = true)
        {
            if (credentials == null)
                throw new ArgumentNullException("credentials");

            IIdentityService identityService = CreateService(credentials, logRequests);
            IAuthenticationService authenticationService;
            switch (credentials.Vendor)
            {
            case "HP":
                // currently HP does not have a vendor-specific IIdentityService
                goto default;

            case "Rackspace":
                authenticationService = new RackspaceAuthenticationService(identityService, credentials.AuthenticationRequest);
                break;

            case "OpenStack":
            default:
                authenticationService = new IdentityV2AuthenticationService(identityService, credentials.AuthenticationRequest);
                break;
            }

            return authenticationService;
        }
        internal static IIdentityService CreateService(TestCredentials credentials, bool logRequests = true)
        {
            if (credentials == null)
                throw new ArgumentNullException("credentials");

            IdentityClient client;
            switch (credentials.Vendor)
            {
            case "HP":
                // currently HP does not have a vendor-specific IIdentityService
                goto default;

            case "Rackspace":
                client = new RackspaceIdentityClient(credentials.BaseAddress);
                break;

            case "OpenStack":
            default:
                client = new IdentityClient(credentials.BaseAddress);
                break;
            }

            TestProxy.ConfigureService(client, credentials.Proxy);
            if (logRequests)
            {
                client.BeforeAsyncWebRequest += TestHelpers.HandleBeforeAsyncWebRequest;
                client.AfterAsyncWebResponse += TestHelpers.HandleAfterAsyncWebResponse;
            }

            return client;
        }
Exemplo n.º 4
0
        public async Task TestAuthenticate()
        {
            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource())
            {
                cancellationTokenSource.CancelAfter(TestTimeout(TimeSpan.FromSeconds(10)));

                using (IIdentityService service = CreateService())
                {
                    TestCredentials credentials = Credentials;
                    Assert.IsNotNull(credentials);

                    AuthenticationRequest request = credentials.AuthenticationRequest;
                    Assert.IsNotNull(request);

                    AuthenticateApiCall apiCall = await service.PrepareAuthenticateAsync(request, cancellationTokenSource.Token);

                    Tuple <HttpResponseMessage, AccessResponse> response = await apiCall.SendAsync(cancellationTokenSource.Token);

                    Assert.IsNotNull(response);
                    Assert.IsNotNull(response.Item2);

                    Access access = response.Item2.Access;
                    Assert.IsNotNull(access);
                    Assert.IsNotNull(access.Token);
                    Assert.IsFalse(access.ServiceCatalog.IsDefault);
                    Assert.IsNotNull(access.User);

                    // check the token
                    Token token = access.Token;
                    Assert.IsNotNull(token);
                    Assert.IsNotNull(token.Id);
                    Assert.IsNotNull(token.Tenant);

                    // Rackspace does not return this property, and it doesn't seem to be particularly useful.
                    //Assert.IsNotNull(token.IssuedAt);

                    Assert.IsNotNull(token.ExpiresAt);

                    // check the user
                    User user = access.User;
                    Assert.IsNotNull(user);
                    Assert.IsNotNull(user.Id);
                    Assert.IsNotNull(user.Name);

                    if (string.Equals(Credentials.Vendor, "rackspace", StringComparison.OrdinalIgnoreCase))
                    {
                        Assert.IsNotNull(user.GetDefaultRegion());
                    }

                    // If the Username is null, it's presumed to be the same as the Name. (Rackspace does not return
                    // this property.)
                    //Assert.IsNotNull(user.Username);

                    Assert.IsFalse(user.Roles.IsDefault);

                    Assert.AreNotEqual(0, user.Roles.Length);
                    foreach (Role role in user.Roles)
                    {
                        Assert.IsNotNull(role);
                        Assert.IsNotNull(role.Name);
                        Assert.AreNotEqual(string.Empty, role.Name);
                    }

                    // At least Rackspace does not return the roles_links property.
                    if (user.RolesLinks != null)
                    {
                        foreach (Link link in user.RolesLinks)
                        {
                            Assert.IsNotNull(link);
                            Assert.IsNotNull(link.Relation);
                            Assert.AreNotEqual(string.Empty, link.Relation);
                            Assert.IsNotNull(link.Target);
                            Assert.IsTrue(link.Target.IsAbsoluteUri);
                        }
                    }

                    // check the service catalog
                    ImmutableArray <ServiceCatalogEntry> serviceCatalog = access.ServiceCatalog;
                    Assert.IsFalse(serviceCatalog.IsDefault);
                    Assert.AreNotEqual(0, serviceCatalog.Length);
                    foreach (ServiceCatalogEntry entry in serviceCatalog)
                    {
                        Assert.IsNotNull(entry);
                        Assert.IsNotNull(entry.Name);
                        Assert.IsNotNull(entry.Type);
                        Assert.IsFalse(entry.Endpoints.IsDefault);

                        Assert.AreNotEqual(0, entry.Endpoints.Length);
                        foreach (Endpoint endpoint in entry.Endpoints)
                        {
                            Assert.IsNotNull(endpoint);

                            // At least Rackspace does not return an ID with their endpoints. The ID doesn't seem
                            // necessary for API calls.
                            //Assert.IsNotNull(endpoint.Id);

                            // Region-independent endpoints may have a null Region value.
                            //Assert.IsNotNull(endpoint.Region);

                            Assert.IsFalse(endpoint.PublicUrl == null && endpoint.InternalUrl == null && endpoint.AdminUrl == null);
                        }

                        // At least Rackspace does not return the endpoints_links property.
                        if (entry.EndpointsLinks != null)
                        {
                            foreach (Link link in entry.EndpointsLinks)
                            {
                                Assert.IsNotNull(link);
                                Assert.IsNotNull(link.Relation);
                                Assert.AreNotEqual(string.Empty, link.Relation);
                                Assert.IsNotNull(link.Target);
                                Assert.IsTrue(link.Target.IsAbsoluteUri);
                            }
                        }
                    }
                }
            }
        }
        internal static IContentDeliveryService CreateService(IAuthenticationService authenticationService, TestCredentials credentials)
        {
            ContentDeliveryClient client;
            switch (credentials.Vendor)
            {
                case "HP":
                    // currently HP does not have a vendor-specific IContentDeliveryService
                    goto default;

                case "Rackspace":
                    // currently Rackspace does not have a vendor-specific IContentDeliveryService
                    goto default;

                case "OpenStack":
                default:
                    client = new ContentDeliveryClient(authenticationService, credentials.DefaultRegion, false);
                    break;
            }

            TestProxy.ConfigureService(client, credentials.Proxy);
            client.BeforeAsyncWebRequest += TestHelpers.HandleBeforeAsyncWebRequest;
            client.AfterAsyncWebResponse += TestHelpers.HandleAfterAsyncWebResponse;

            return client;
        }