def test_get(self): """Get a specific OpenStack cloud from a tenant.""" # The clouds in this test. TENANT_CLOUD = [{"tenant_name": 'a', "username": '******', "password": '******', "auth_url": "http://d.com"}, {"tenant_name": "ee", "username": "******", "password": "******", "auth_url": "http://route66.com"}, ] # Make a tenant. tenant = Tenant.objects.create(name='tenant 1', owner='John', owner_contact='206.867.5309') # Create a tenant_admin of the tenant. token = create_and_login(tenant=tenant) # For every test cloud... for entry in TENANT_CLOUD: # Make it. cloud = Cloud.objects.create(tenant=tenant, **entry) # Try GETting it. response = self.client.get( TENANTS_ID_CLOUD_ID_URL % (tenant.uuid, cloud.uuid), HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, entry)
def test_post(self): """Create an OpenStack cloud in a tenant.""" # The clouds in this test. TENANT_CLOUD = [{"tenant_name": 'a', "username": '******', "password": '******', "auth_url": "http://d.com"}, {"tenant_name": "ee", "username": "******", "password": "******", "auth_url": "http://route66.com"}, ] # Make a tenant tenant = Tenant.objects.create(name='tenant', owner='John', owner_contact='206.867.5309') # Create a user who's the tenant_admin of this tenant, and log him in. token = create_and_login(tenant=tenant) # Create OpenStack clouds in this tenant, and check the results. for entry in TENANT_CLOUD: response = self.client.post( TENANTS_ID_CLOUD_URL % tenant.uuid, json.dumps(entry), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_201_CREATED, entry)
def test_change_some_fields(self): """Get data from an account, after we've modified some fields.""" expected_content = deepcopy(EXPECTED_CONTENT) expected_content["first_name"] = "Dirk" expected_content["last_name"] = "Diggler" # Create a user and get their authorization token. token = create_and_login() # Change some attributes from the default. Note, the username is # required by djoser UserView/PUT. response = self.client.put( USER_URL, json.dumps({"username": TEST_USER[0], "first_name": "Dirk", "last_name": "Diggler"}), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # pylint: disable=E1101 self.assertEqual(response.status_code, HTTP_200_OK) # Now get the account attributes and see if they've changed. response = self.client.get( USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, expected_content, extra_keys=["last_login", "date_joined"])
def test_no_tenant(self): """Get or change Cloud data when there's no Goldstone tenant.""" expected_content = deepcopy(EXPECTED_CONTENT) expected_content["tenant_admin"] = True # Make a tenant tenant = Tenant.objects.create(name='hellothere', owner='John', owner_contact='206.867.5309') # Create a tenant_admin of the tenant. Then nullify the Tenant field. token = create_and_login(tenant=tenant) user = get_user_model().objects.all()[0] user.tenant = None user.save() response = self.client.get( USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, expected_content, extra_keys=["last_login", "date_joined"])
def test_get_restricted_fields(self): """Try getting fields that are restricted to tenant_admins.""" cloud_fields = {"tenant_name": "cloud name 0", "username": "******", "password": "******", "auth_url": "http://10.11.12.13:5000/v3/"} # Make a tenant, and one Cloud under it. tenant = Tenant.objects.create(name='hellothere', owner='John', owner_contact='206.867.5309') cloud_fields["tenant"] = tenant Cloud.objects.create(**cloud_fields) # Create a normal user. token = create_and_login() response = self.client.get( USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # None of the Tenant or Cloud fields should be in the response. check_response_without_uuid(response, HTTP_200_OK, EXPECTED_CONTENT, extra_keys=["last_login", "date_joined"])
def test_put(self): """Update a user in a tenant.""" # Expected response, sans uuid. EXPECTED_RESPONSE = { "username": "******", "first_name": "1", "last_name": "2", "email": "*****@*****.**", "tenant_admin": False, "is_superuser": False, "default_tenant_admin": False, } # Make a tenant. tenant = Tenant.objects.create(name="tenant", owner="John", owner_contact="206.867.5309") # Create a tenant_admin of the tenant, and a normal user of the tenant. token = create_and_login(tenant=tenant) user = get_user_model().objects.create_user(username="******", password="******") user.tenant = tenant user.save() # Try PUTing to the user. response = self.client.put( TENANTS_ID_USERS_ID_URL % (tenant.uuid, user.uuid), json.dumps({"username": "******", "first_name": "1", "last_name": "2", "email": "*****@*****.**"}), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token, ) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_RESPONSE, extra_keys=["last_login", "date_joined"])
def test_change_some_fields(self): """Get data from an account, after we've modified some fields.""" expected_content = deepcopy(EXPECTED_CONTENT) expected_content["first_name"] = "Dirk" expected_content["last_name"] = "Diggler" # Create a user and get their authorization token. token = create_and_login() # Change some attributes from the default. Note, the username is # required by djoser UserView/PUT. response = self.client.put(USER_URL, json.dumps({ "username": TEST_USER[0], "first_name": "Dirk", "last_name": "Diggler" }), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # pylint: disable=E1101 self.assertEqual(response.status_code, HTTP_200_OK) # Now get the account attributes and see if they've changed. response = self.client.get(USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, expected_content, extra_keys=["last_login", "date_joined"])
def test_get_restricted_fields(self): """Try getting fields that are restricted to tenant_admins.""" cloud_fields = { "tenant_name": "cloud name 0", "username": "******", "password": "******", "auth_url": "http://10.11.12.13:5000/v3/" } # Make a tenant, and one Cloud under it. tenant = Tenant.objects.create(name='hellothere', owner='John', owner_contact='206.867.5309') cloud_fields["tenant"] = tenant Cloud.objects.create(**cloud_fields) # Create a normal user. token = create_and_login() response = self.client.get(USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # None of the Tenant or Cloud fields should be in the response. check_response_without_uuid(response, HTTP_200_OK, EXPECTED_CONTENT, extra_keys=["last_login", "date_joined"])
def create(user_number): """Create one user in the tenant. :param user_number: The TENANT_USERS index to use :type user_number: int """ from django.conf import settings response = self.client.post( TENANTS_ID_USERS_URL % tenant.uuid, json.dumps(TENANT_USERS[user_number]), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token, ) check_response_without_uuid( response, HTTP_201_CREATED, EXPECTED_RESULT[user_number], extra_keys=["last_login", "date_joined"] ) # Was email send to the new user? self.assertEqual(send_email.call_count, 1) # Did the e-mail seem to have the correct content? self.assertEqual(send_email.call_args[0][0], TENANT_USERS[user_number]["email"]) self.assertEqual(send_email.call_args[0][1], "webmaster@localhost") # from self.assertEqual(send_email.call_args[0][2]["site_name"], settings.DJOSER["SITE_NAME"]) # The site name self.assertIn("tenant", send_email.call_args[0][2]["tenant_name"]) self.assertEqual( send_email.call_args[1], {"plain_body_template_name": "new_tenant_body.txt", "subject_template_name": "new_tenant.txt"}, )
def test_get(self): """Get a user.""" # Expected results, sans uuid keys. EXPECTED_RESULTS = [ { "username": "******", "first_name": "", "last_name": "", "email": "*****@*****.**", "tenant_admin": True, "is_superuser": False, "tenant_name": "tennent", "default_tenant_admin": False }, { "username": "******", "first_name": "", "last_name": "", "email": '', "tenant_admin": False, "is_superuser": False, "default_tenant_admin": False }, ] # Make a tenant. tenant = Tenant.objects.create(name='tennent', owner='John', owner_contact='206.867.5309') # Create a tenant_admin of the tenant. token = create_and_login(tenant=tenant) user = get_user_model().objects.get(username=TEST_USER[0]) # Try GETing the tenant admin. response = self.client.get( TENANTS_ID_USERS_ID_URL % (tenant.uuid, user.uuid), HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_RESULTS[0], extra_keys=["last_login", "date_joined"]) # Add another user to the tenant, and get her. user = get_user_model().objects.create_user(username="******", password='******') user.tenant = tenant user.save() # Try GETing the second user. response = self.client.get( TENANTS_ID_USERS_ID_URL % (tenant.uuid, user.uuid), HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_RESULTS[1], extra_keys=["last_login", "date_joined"])
def test_no_tenant(self): """Get or change Cloud data when there's no Goldstone tenant.""" expected_content = deepcopy(EXPECTED_CONTENT) expected_content["tenant_admin"] = True # Make a tenant tenant = Tenant.objects.create(name='hellothere', owner='John', owner_contact='206.867.5309') # Create a tenant_admin of the tenant. Then nullify the Tenant field. token = create_and_login(tenant=tenant) user = get_user_model().objects.all()[0] user.tenant = None user.save() response = self.client.get(USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, expected_content, extra_keys=["last_login", "date_joined"])
def test_get_django_admin(self): """Get a tenant list when tenants exist, as a Django admin.""" # The expected content, sans uuids. EXPECTED_CONTENT = \ {'count': 2, 'next': None, 'previous': None, 'results': [{'name': 'tenant 1', 'owner': 'John', 'owner_contact': ''}, {'name': 'tenant 2', 'owner': 'Alex', 'owner_contact': '867-5309'}]} # Create a Django admin user and log her in. token = create_and_login(is_superuser=True) # Make two tenants. Tenant.objects.create(name=EXPECTED_CONTENT["results"][0]["name"], owner=EXPECTED_CONTENT["results"][0]["owner"]) Tenant.objects.create( name=EXPECTED_CONTENT["results"][1]["name"], owner=EXPECTED_CONTENT["results"][1]["owner"], owner_contact=EXPECTED_CONTENT["results"][1]["owner_contact"]) # Try getting the list, then check the results. response = self.client.get( TENANTS_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_CONTENT, True)
def test_put(self): """Update an Openstack cloud in a tenant.""" # The cloud in this test. TENANT_CLOUD = {"tenant_name": 'a', "username": '******', "password": '******', "auth_url": "http://d.com"} EXPECTED_RESPONSE = TENANT_CLOUD.copy() EXPECTED_RESPONSE["password"] = "******" # Make a tenant, put an OpenStack cloud in it. tenant = Tenant.objects.create(name='tenant 1', owner='John', owner_contact='206.867.5309') cloud = Cloud.objects.create(tenant=tenant, **TENANT_CLOUD) # Create a tenant_admin of the tenant. token = create_and_login(tenant=tenant) # Try PUTing to the cloud. response = self.client.put( TENANTS_ID_CLOUD_ID_URL % (tenant.uuid, cloud.uuid), json.dumps(EXPECTED_RESPONSE), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_RESPONSE) # Double-check that the Cloud row was updated. self.assertEqual(Cloud.objects.count(), 1) self.assertEqual(Cloud.objects.all()[0].password, EXPECTED_RESPONSE["password"])
def get_tenant(token, expected): """Get the tenant using the token and check the response.""" response = self.client.get( TENANTS_ID_URL % tenant.uuid, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, expected)
def test_get(self): """Get a user.""" # Expected results, sans uuid keys. EXPECTED_RESULTS = [{"username": "******", "first_name": "", "last_name": "", "email": "*****@*****.**", "tenant_admin": True, "is_superuser": False, "tenant_name": "tennent", "default_tenant_admin": False}, {"username": "******", "first_name": "", "last_name": "", "email": '', "tenant_admin": False, "is_superuser": False, "default_tenant_admin": False}, ] # Make a tenant. tenant = Tenant.objects.create(name='tennent', owner='John', owner_contact='206.867.5309') # Create a tenant_admin of the tenant. token = create_and_login(tenant=tenant) user = get_user_model().objects.get(username=TEST_USER_1[0]) # Try GETing the tenant admin. response = self.client.get( TENANTS_ID_USERS_ID_URL % (tenant.uuid, user.uuid), HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_RESULTS[0], extra_keys=["last_login", "date_joined"]) # Add another user to the tenant, and get her. user = get_user_model().objects.create_user(username="******", password='******') user.tenant = tenant user.save() # Try GETing the second user. response = self.client.get( TENANTS_ID_USERS_ID_URL % (tenant.uuid, user.uuid), HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_RESULTS[1], extra_keys=["last_login", "date_joined"])
def test_put(self): """Change a tenant's attributes.""" # The expected result, sans uuid. INITIAL_TENANT = { "name": "tenant", "owner": "John", "owner_contact": "206.867.5309" } NEW_TENANT = { "name": "TENant", "owner": "Bob", "owner_contact": "212.867.5309" } def get_tenant(token, expected): """Get the tenant using the token and check the response.""" response = self.client.get( TENANTS_ID_URL % tenant.uuid, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, expected) # Make a tenant tenant = Tenant.objects.create(**INITIAL_TENANT) # Create a Django admin user, and a normal user who's a tenant_admin of # the tenant. token = create_and_login(is_superuser=True) get_user_model().objects.create_user("a", "*****@*****.**", "a", tenant=tenant, tenant_admin=True) # Test changing the tenant as a Django admin. response = self.client.put(TENANTS_ID_URL % tenant.uuid, json.dumps(NEW_TENANT), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, NEW_TENANT) get_tenant(token, NEW_TENANT) # Test changing the tenant as a tenant admin. token = login('a', 'a') response = self.client.put(TENANTS_ID_URL % tenant.uuid, json.dumps(NEW_TENANT), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, NEW_TENANT) get_tenant(token, NEW_TENANT)
def test_change_some_fields(self): """Get Cloud data and change some fields.""" cloud_fields = { "tenant_name": "cloud name 0", "username": "******", "password": "******", "auth_url": "http://10.11.12.13:5000/v3/" } # Make a tenant, and one Cloud under it. tenant = Tenant.objects.create(name='hellothere', owner='John', owner_contact='206.867.5309') cloud_fields["tenant"] = tenant Cloud.objects.create(**cloud_fields) # Create a tenant_admin of the tenant. token = create_and_login(tenant=tenant) # Change some attributes. Note, the username is required by djoser # UserView/PUT. response = self.client.put( USER_URL, json.dumps({ "username": TEST_USER[0], "os_username": "******", "os_password": "******", "tenant_name": "$20k" }), # tenant_name won't change. content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # pylint: disable=E1101 self.assertEqual(response.status_code, HTTP_200_OK) # Now get the account attributes and see if they've changed. response = self.client.get(USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # Concoct the results we expect, which includes the cloud credentials. expected_content = deepcopy(EXPECTED_CONTENT) expected_content["tenant_admin"] = True expected_content["tenant_name"] = tenant.name expected_content["os_name"] = cloud_fields["tenant_name"] expected_content["os_username"] = "******" expected_content["os_password"] = "******" expected_content["os_auth_url"] = cloud_fields["auth_url"] check_response_without_uuid(response, HTTP_200_OK, expected_content, extra_keys=["last_login", "date_joined"])
def test_get_tenant_admin(self): """Get a tenant list when tenants exist, as a tenant_admin.""" # The expected content, sans uuids. EXPECTED_CONTENT = { 'count': 1, 'next': None, 'previous': None, 'results': [{ 'name': 'tenant 1', 'owner': 'John', 'owner_contact': '' }], } # The tenants we'll create. The first one will be owned by the # tenant_admin, and the rest will not. TENANTS = [ { "name": EXPECTED_CONTENT["results"][0]["name"], "owner": EXPECTED_CONTENT["results"][0]["owner"] }, { "name": "Mary Louise", "owner": "Parker" }, { "name": "Angelina", "owner": "Jolie" }, { "name": "Debra", "owner": "Winger" }, ] # Make four tenants. tenants = [Tenant.objects.create(**x) for x in TENANTS] # Create a tenant_admin user and log her in. token = create_and_login(tenant=tenants[0]) # Get the list of tenants. Only one being adminstered by the # tenant_admin should be in the list. response = self.client.get(TENANTS_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_CONTENT, True)
def test_put(self): """Change a tenant's attributes.""" # The expected result, sans uuid. INITIAL_TENANT = {"name": "tenant", "owner": "John", "owner_contact": "206.867.5309"} NEW_TENANT = {"name": "TENant", "owner": "Bob", "owner_contact": "212.867.5309"} def get_tenant(token, expected): """Get the tenant using the token and check the response.""" response = self.client.get( TENANTS_ID_URL % tenant.uuid, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, expected) # Make a tenant tenant = Tenant.objects.create(**INITIAL_TENANT) # Create a Django admin user, and a normal user who's a tenant_admin of # the tenant. token = create_and_login(is_superuser=True) get_user_model().objects.create_user("a", "*****@*****.**", "a", tenant=tenant, tenant_admin=True) # Test changing the tenant as a Django admin. response = self.client.put( TENANTS_ID_URL % tenant.uuid, json.dumps(NEW_TENANT), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, NEW_TENANT) get_tenant(token, NEW_TENANT) # Test changing the tenant as a tenant admin. token = login('a', 'a') response = self.client.put( TENANTS_ID_URL % tenant.uuid, json.dumps(NEW_TENANT), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, NEW_TENANT) get_tenant(token, NEW_TENANT)
def test_change_some_fields(self): """Get Cloud data and change some fields.""" cloud_fields = {"tenant_name": "cloud name 0", "username": "******", "password": "******", "auth_url": "http://10.11.12.13:5000/v3/"} # Make a tenant, and one Cloud under it. tenant = Tenant.objects.create(name='hellothere', owner='John', owner_contact='206.867.5309') cloud_fields["tenant"] = tenant Cloud.objects.create(**cloud_fields) # Create a tenant_admin of the tenant. token = create_and_login(tenant=tenant) # Change some attributes. Note, the username is required by djoser # UserView/PUT. response = self.client.put( USER_URL, json.dumps({"username": TEST_USER[0], "os_username": "******", "os_password": "******", "tenant_name": "$20k"}), # tenant_name won't change. content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # pylint: disable=E1101 self.assertEqual(response.status_code, HTTP_200_OK) # Now get the account attributes and see if they've changed. response = self.client.get( USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # Concoct the results we expect, which includes the cloud credentials. expected_content = deepcopy(EXPECTED_CONTENT) expected_content["tenant_admin"] = True expected_content["tenant_name"] = tenant.name expected_content["os_name"] = cloud_fields["tenant_name"] expected_content["os_username"] = "******" expected_content["os_password"] = "******" expected_content["os_auth_url"] = cloud_fields["auth_url"] check_response_without_uuid(response, HTTP_200_OK, expected_content, extra_keys=["last_login", "date_joined"])
def test_get(self): # pylint: disable=R0201 """Get data from the default created account.""" # Create a user and get their authorization token. token = create_and_login() response = self.client.get( USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_CONTENT, extra_keys=["last_login", "date_joined"])
def test_get(self): # pylint: disable=R0201 """Get data from the default created account.""" # Create a user and get their authorization token. token = create_and_login() response = self.client.get(USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_CONTENT, extra_keys=["last_login", "date_joined"])
def test_post_restricted_fields(self): """Try changing fields that are restricted to tenant_admins.""" cloud_fields = { "tenant_name": "cloud name 0", "username": "******", "password": "******", "auth_url": "http://10.11.12.13:5000/v3/" } # Make a tenant, and one Cloud under it. tenant = Tenant.objects.create(name='hellothere', owner='John', owner_contact='206.867.5309') cloud_fields["tenant"] = tenant Cloud.objects.create(**cloud_fields) # Create a normal user. token = create_and_login() # Try to change some attributes. Note, the username is required by # djoser UserView/PUT. response = self.client.put( USER_URL, json.dumps({ "username": TEST_USER[0], "os_username": "******", "os_password": "******", "tenant_name": "$20k" }), # tenant_name won't change. content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # pylint: disable=E1101 self.assertEqual(response.status_code, HTTP_200_OK) # Now get the account attributes and see if they've changed. response = self.client.get(USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # None of the Tenant or Cloud fields should be in the response. check_response_without_uuid(response, HTTP_200_OK, EXPECTED_CONTENT, extra_keys=["last_login", "date_joined"])
def test_put(self): """Update a user in a tenant.""" # Expected response, sans uuid. EXPECTED_RESPONSE = { "username": "******", "first_name": "1", "last_name": "2", "email": "*****@*****.**", "tenant_admin": False, "is_superuser": False, "default_tenant_admin": False } # Make a tenant. tenant = Tenant.objects.create(name='tenant', owner='John', owner_contact='206.867.5309') # Create a tenant_admin of the tenant, and a normal user of the tenant. token = create_and_login(tenant=tenant) user = get_user_model().objects.create_user(username="******", password='******') user.tenant = tenant user.save() # Try PUTing to the user. response = self.client.put( TENANTS_ID_USERS_ID_URL % (tenant.uuid, user.uuid), json.dumps({ "username": "******", "first_name": '1', "last_name": '2', "email": "*****@*****.**" }), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_RESPONSE, extra_keys=["last_login", "date_joined"])
def test_post_restricted_fields(self): """Try changing fields that are restricted to tenant_admins.""" cloud_fields = {"tenant_name": "cloud name 0", "username": "******", "password": "******", "auth_url": "http://10.11.12.13:5000/v3/"} # Make a tenant, and one Cloud under it. tenant = Tenant.objects.create(name='hellothere', owner='John', owner_contact='206.867.5309') cloud_fields["tenant"] = tenant Cloud.objects.create(**cloud_fields) # Create a normal user. token = create_and_login() # Try to change some attributes. Note, the username is required by # djoser UserView/PUT. response = self.client.put( USER_URL, json.dumps({"username": TEST_USER[0], "os_username": "******", "os_password": "******", "tenant_name": "$20k"}), # tenant_name won't change. content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # pylint: disable=E1101 self.assertEqual(response.status_code, HTTP_200_OK) # Now get the account attributes and see if they've changed. response = self.client.get( USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # None of the Tenant or Cloud fields should be in the response. check_response_without_uuid(response, HTTP_200_OK, EXPECTED_CONTENT, extra_keys=["last_login", "date_joined"])
def test_get_data(self): """Get Cloud data.""" cloud_fields = { "tenant_name": "cloud name 0", "username": "******", "password": "******", "auth_url": "http://10.11.12.13:5000/v3/" } # Make a tenant, and one Cloud under it. tenant = Tenant.objects.create(name='hellothere', owner='John', owner_contact='206.867.5309') cloud_fields["tenant"] = tenant Cloud.objects.create(**cloud_fields) # Create a tenant_admin of the tenant. token = create_and_login(tenant=tenant) response = self.client.get(USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # Concoct the results we expect, which includes the cloud credentials. expected_content = deepcopy(EXPECTED_CONTENT) expected_content["tenant_admin"] = True expected_content["tenant_name"] = tenant.name expected_content["os_name"] = cloud_fields["tenant_name"] expected_content["os_username"] = cloud_fields["username"] expected_content["os_password"] = cloud_fields["password"] expected_content["os_auth_url"] = cloud_fields["auth_url"] check_response_without_uuid(response, HTTP_200_OK, expected_content, extra_keys=["last_login", "date_joined"])
def test_no_cloud(self): """Get or change Cloud data when there's no Cloud.""" expected_content = deepcopy(EXPECTED_CONTENT) expected_content["tenant_admin"] = True expected_content["tenant_name"] = "hellothere" # Make a tenant. Don't make a Cloud under it. tenant = Tenant.objects.create(name='hellothere', owner='John', owner_contact='206.867.5309') # Create a tenant_admin of the tenant. token = create_and_login(tenant=tenant) response = self.client.get(USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, expected_content, extra_keys=["last_login", "date_joined"])
def test_no_cloud(self): """Get or change Cloud data when there's no Cloud.""" expected_content = deepcopy(EXPECTED_CONTENT) expected_content["tenant_admin"] = True expected_content["tenant_name"] = "hellothere" # Make a tenant. Don't make a Cloud under it. tenant = Tenant.objects.create(name='hellothere', owner='John', owner_contact='206.867.5309') # Create a tenant_admin of the tenant. token = create_and_login(tenant=tenant) response = self.client.get( USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, expected_content, extra_keys=["last_login", "date_joined"])
def create(user_number): """Create one user in the tenant. :param user_number: The TENANT_USERS index to use :type user_number: int """ from django.conf import settings response = self.client.post( TENANTS_ID_USERS_URL % tenant.uuid, json.dumps(TENANT_USERS[user_number]), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid( response, HTTP_201_CREATED, EXPECTED_RESULT[user_number], extra_keys=["last_login", "date_joined"]) # Was email send to the new user? self.assertEqual(send_email.call_count, 1) # Did the e-mail seem to have the correct content? self.assertEqual(send_email.call_args[0][0], TENANT_USERS[user_number]["email"]) self.assertEqual(send_email.call_args[0][1], "webmaster@localhost") # from self.assertEqual(send_email.call_args[0][2]["site_name"], settings.DJOSER["SITE_NAME"]) # The site name self.assertIn("tenant", send_email.call_args[0][2]["tenant_name"]) self.assertEqual( send_email.call_args[1], { 'plain_body_template_name': 'new_tenant_body.txt', 'subject_template_name': 'new_tenant.txt' })
def test_get_tenant_admin(self): """Get a tenant list when tenants exist, as a tenant_admin.""" # The expected content, sans uuids. EXPECTED_CONTENT = {'count': 1, 'next': None, 'previous': None, 'results': [{'name': 'tenant 1', 'owner': 'John', 'owner_contact': ''}], } # The tenants we'll create. The first one will be owned by the # tenant_admin, and the rest will not. TENANTS = [{"name": EXPECTED_CONTENT["results"][0]["name"], "owner": EXPECTED_CONTENT["results"][0]["owner"]}, {"name": "Mary Louise", "owner": "Parker"}, {"name": "Angelina", "owner": "Jolie"}, {"name": "Debra", "owner": "Winger"}, ] # Make four tenants. tenants = [Tenant.objects.create(**x) for x in TENANTS] # Create a tenant_admin user and log her in. token = create_and_login(tenant=tenants[0]) # Get the list of tenants. Only one being adminstered by the # tenant_admin should be in the list. response = self.client.get( TENANTS_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_CONTENT, True)
def test_get_data(self): """Get Cloud data.""" cloud_fields = {"tenant_name": "cloud name 0", "username": "******", "password": "******", "auth_url": "http://10.11.12.13:5000/v3/"} # Make a tenant, and one Cloud under it. tenant = Tenant.objects.create(name='hellothere', owner='John', owner_contact='206.867.5309') cloud_fields["tenant"] = tenant Cloud.objects.create(**cloud_fields) # Create a tenant_admin of the tenant. token = create_and_login(tenant=tenant) response = self.client.get( USER_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # Concoct the results we expect, which includes the cloud credentials. expected_content = deepcopy(EXPECTED_CONTENT) expected_content["tenant_admin"] = True expected_content["tenant_name"] = tenant.name expected_content["os_name"] = cloud_fields["tenant_name"] expected_content["os_username"] = cloud_fields["username"] expected_content["os_password"] = cloud_fields["password"] expected_content["os_auth_url"] = cloud_fields["auth_url"] check_response_without_uuid(response, HTTP_200_OK, expected_content, extra_keys=["last_login", "date_joined"])
def test_put_bad_fields(self): """Update a user with missing required fields, or unrecognized fields, or a field that's not allowed to be changed by the tenant_admin.""" # Expected responses, sans uuid keys. EXPECTED_RESPONSES = [ # PUTting no changes. {"username": "******", "first_name": "", "last_name": "", "email": "", "tenant_admin": False, "is_superuser": False, "default_tenant_admin": False}, # PUTting to an unrecognized field. {"username": "******", "first_name": "Michelle", "last_name": "", "email": "", "tenant_admin": False, "is_superuser": False, "default_tenant_admin": False}, ] # Make a tenant. tenant = Tenant.objects.create(name='tenant', owner='John', owner_contact='206.867.5309') # Create a tenant_admin of the tenant, and a normal user of the tenant. token = create_and_login(tenant=tenant) user = get_user_model().objects.create_user(username="******", password='******') user.tenant = tenant user.save() # Try PUTing to the user with no username. response = self.client.put( TENANTS_ID_USERS_ID_URL % (tenant.uuid, user.uuid), HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) self.assertContains(response, CONTENT_NOT_BLANK_USERNAME, status_code=HTTP_400_BAD_REQUEST) # Try PUTing to the user with no changes, and with a change to an # unrecognized field. for i, entry in enumerate([{"username": "******"}, {"username": "******", "billybopfoo": "blaRGH", "first_name": "Michelle"}, ]): response = self.client.put( TENANTS_ID_USERS_ID_URL % (tenant.uuid, user.uuid), json.dumps(entry), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_RESPONSES[i], extra_keys=["last_login", "date_joined"]) # Try PUTing to the user on a field that's not allowed to be changed. # The response should be the same as the "unrecognized field" case. response = self.client.put( TENANTS_ID_USERS_ID_URL % (tenant.uuid, user.uuid), json.dumps({"username": "******", "billybopfoo": "blaRGH", "tenant_admin": True, "default_tenant_admin": True, "first_name": "Michelle"}), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_RESPONSES[1], extra_keys=["last_login", "date_joined"])
def test_put_bad_fields(self): """Update a user with missing required fields, or unrecognized fields, or a field that's not allowed to be changed by the tenant_admin.""" # Expected responses, sans uuid keys. EXPECTED_RESPONSES = [ # PUTting no changes. { "username": "******", "first_name": "", "last_name": "", "email": "", "tenant_admin": False, "is_superuser": False, "default_tenant_admin": False }, # PUTting to an unrecognized field. { "username": "******", "first_name": "Michelle", "last_name": "", "email": "", "tenant_admin": False, "is_superuser": False, "default_tenant_admin": False }, ] # Make a tenant. tenant = Tenant.objects.create(name='tenant', owner='John', owner_contact='206.867.5309') # Create a tenant_admin of the tenant, and a normal user of the tenant. token = create_and_login(tenant=tenant) user = get_user_model().objects.create_user(username="******", password='******') user.tenant = tenant user.save() # Try PUTing to the user with no username. response = self.client.put( TENANTS_ID_USERS_ID_URL % (tenant.uuid, user.uuid), HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) self.assertContains(response, CONTENT_NOT_BLANK_USERNAME, status_code=HTTP_400_BAD_REQUEST) # Try PUTing to the user with no changes, and with a change to an # unrecognized field. for i, entry in enumerate([ { "username": "******" }, { "username": "******", "billybopfoo": "blaRGH", "first_name": "Michelle" }, ]): response = self.client.put( TENANTS_ID_USERS_ID_URL % (tenant.uuid, user.uuid), json.dumps(entry), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid( response, HTTP_200_OK, EXPECTED_RESPONSES[i], extra_keys=["last_login", "date_joined"]) # Try PUTing to the user on a field that's not allowed to be changed. # The response should be the same as the "unrecognized field" case. response = self.client.put( TENANTS_ID_USERS_ID_URL % (tenant.uuid, user.uuid), json.dumps({ "username": "******", "billybopfoo": "blaRGH", "tenant_admin": True, "default_tenant_admin": True, "first_name": "Michelle" }), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, EXPECTED_RESPONSES[1], extra_keys=["last_login", "date_joined"])
def test_put_bad_fields(self): """Update an OpenStack cloud with missing fields, unrecognized fields, or a field that's not allowed to be changed by the tenant_admin.""" # The cloud in this test. TENANT_CLOUD = {"tenant_name": 'a', "username": '******', "password": '******', "auth_url": "http://d.com"} # Make a tenant, put an OpenStack cloud in it. tenant = Tenant.objects.create(name='tenant 1', owner='John', owner_contact='206.867.5309') cloud = Cloud.objects.create(tenant=tenant, **TENANT_CLOUD) # Create a tenant_admin of the tenant. token = create_and_login(tenant=tenant) # Try PUTing to the cloud with no fields. response = self.client.put( TENANTS_ID_CLOUD_ID_URL % (tenant.uuid, cloud.uuid), HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) for content in [CONTENT_MISSING_OS_USERNAME, CONTENT_MISSING_OS_NAME, CONTENT_MISSING_OS_PASSWORD, CONTENT_MISSING_OS_URL]: self.assertContains(response, content, status_code=HTTP_400_BAD_REQUEST) # Try PUTing to the cloud with no change, and with a change to an # unrecognized field. response = self.client.put( TENANTS_ID_CLOUD_ID_URL % (tenant.uuid, cloud.uuid), json.dumps(TENANT_CLOUD), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, TENANT_CLOUD) bad_field = TENANT_CLOUD.copy() bad_field["forkintheroad"] = "Traci" response = self.client.put( TENANTS_ID_CLOUD_ID_URL % (tenant.uuid, cloud.uuid), json.dumps(bad_field), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, TENANT_CLOUD) # Try PUTing to a cloud on a field that's not allowed to be changed. # The response should be the same as the "unrecognized field" case. bad_field = TENANT_CLOUD.copy() bad_field["uuid"] = BAD_UUID response = self.client.put( TENANTS_ID_CLOUD_ID_URL % (tenant.uuid, cloud.uuid), json.dumps(bad_field), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_200_OK, TENANT_CLOUD)
def test_post(self, number_tenant_admins=1): """Create a tenant. :param number_tenant_admin: The number of default_tenant_admins to use in this test. 0 = use none. 1 = use the normal number, i.e., one. > 1 = use this number, and the system should use one of them when it creates a tenant. """ from django.conf import settings # The expected content, sans uuids. EXPECTED_CONTENT = \ {'count': 3, 'next': None, 'previous': None, 'results': [{'name': 'tenant 1', 'owner': 'John', 'owner_contact': 'eight six seven'}, {'name': 'tenant 2', 'owner': 'Alex', 'owner_contact': 'five three oh niiieeiiiin...'}, {'name': 'tenant 3', 'owner': 'James T. Kirk', 'owner_contact': '(666)666-6666'}]} # Create a user, save the authorization token, and make the user a # Django admin. token = create_and_login(is_superuser=True) # Create the desired number of default_tenant_admins. if number_tenant_admins == 0: # Run this test with no default tenant admins. The "current" user, # who's a Django admin, should be used as the default tenant_admin. default_tenant_admins = \ [get_user_model().objects.get(username=TEST_USER_1[0])] else: # Run this test with one or more default_tenant_admins. default_tenant_admins = [get_user_model().objects.create_user( "Julianne_%d" % x, "*****@*****.**", "bueno", default_tenant_admin=True) for x in range(number_tenant_admins)] with patch("djoser.utils.send_email") as send_email: # Make the tenants, and check each POST's response. for entry in EXPECTED_CONTENT["results"]: response = self.client.post( TENANTS_URL, json.dumps(entry), content_type="application/json", HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) check_response_without_uuid(response, HTTP_201_CREATED, entry) # Now get the list and see if it matches what we expect. response = self.client.get( TENANTS_URL, HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token) # Check the response. check_response_without_uuid(response, HTTP_200_OK, EXPECTED_CONTENT, True) # Did the ViewSet attempt to send three emails? self.assertEqual(send_email.call_count, 3) # Did the e-mails seem to have the correct content? for entry in [0, 1, 2]: # tenant_admin email. self.assertIn(send_email.call_args_list[entry][0][0], [x.email for x in default_tenant_admins]) # from self.assertEqual(send_email.call_args_list[entry][0][1], "webmaster@localhost") # site name self.assertEqual( send_email.call_args_list[entry][0][2]["site_name"], settings.DJOSER["SITE_NAME"]) # The name of the newly created tenant. self.assertEqual( send_email.call_args_list[entry][0][2]["tenant_name"], EXPECTED_CONTENT["results"][entry]["name"])