Fix tests to pass when using PostgreSQL as the test database.

This commit is contained in:
Chris Church
2013-06-16 00:06:56 -04:00
parent ca4050aee8
commit f9b385bed2
5 changed files with 149 additions and 150 deletions

View File

@@ -215,8 +215,7 @@ class AcomInventoryTest(BaseCommandTest):
# Host without variable data. # Host without variable data.
inventory = self.inventories[0] inventory = self.inventories[0]
self.assertTrue(inventory.active) self.assertTrue(inventory.active)
host = inventory.hosts.all()[2] host = inventory.hosts.filter(active=True)[2]
self.assertTrue(host.active)
os.environ['ACOM_INVENTORY_ID'] = str(inventory.pk) os.environ['ACOM_INVENTORY_ID'] = str(inventory.pk)
result, stdout, stderr = self.run_command('acom_inventory', result, stdout, stderr = self.run_command('acom_inventory',
host=host.name) host=host.name)
@@ -226,8 +225,7 @@ class AcomInventoryTest(BaseCommandTest):
# Host with variable data. # Host with variable data.
inventory = self.inventories[1] inventory = self.inventories[1]
self.assertTrue(inventory.active) self.assertTrue(inventory.active)
host = inventory.hosts.all()[4] host = inventory.hosts.filter(active=True)[4]
self.assertTrue(host.active)
os.environ['ACOM_INVENTORY_ID'] = str(inventory.pk) os.environ['ACOM_INVENTORY_ID'] = str(inventory.pk)
result, stdout, stderr = self.run_command('acom_inventory', result, stdout, stderr = self.run_command('acom_inventory',
host=host.name) host=host.name)

View File

@@ -5,8 +5,7 @@ import datetime
import json import json
from django.contrib.auth.models import User as DjangoUser from django.contrib.auth.models import User as DjangoUser
import django.test from django.core.urlresolvers import reverse
from django.test.client import Client
from ansibleworks.main.models import * from ansibleworks.main.models import *
from ansibleworks.main.tests.base import BaseTest from ansibleworks.main.tests.base import BaseTest
@@ -47,12 +46,11 @@ class InventoryTest(BaseTest):
def test_main_line(self): def test_main_line(self):
# some basic URLs... # some basic URLs...
inventories = '/api/v1/inventories/' inventories = reverse('main:inventory_list')
inventories_1 = '/api/v1/inventories/1/' inventories_1 = reverse('main:inventory_detail', args=(self.inventory_a.pk,))
inventories_2 = '/api/v1/inventories/2/' inventories_2 = reverse('main:inventory_detail', args=(self.inventory_b.pk,))
hosts = '/api/v1/hosts/' hosts = reverse('main:host_list')
groups = '/api/v1/groups/' groups = reverse('main:group_list')
variables = '/api/v1/variables/'
# a super user can list inventories # a super user can list inventories
data = self.get(inventories, expect=200, auth=self.get_super_credentials()) data = self.get(inventories, expect=200, auth=self.get_super_credentials())
@@ -88,22 +86,23 @@ class InventoryTest(BaseTest):
data = self.get(inventories_2, expect=403, auth=self.get_nobody_credentials()) data = self.get(inventories_2, expect=403, auth=self.get_nobody_credentials())
# a super user can create inventory # a super user can create inventory
new_inv_1 = dict(name='inventory-c', description='baz', organization=1) new_inv_1 = dict(name='inventory-c', description='baz', organization=self.organizations[0].pk)
new_id = max(Inventory.objects.values_list('pk', flat=True)) + 1
data = self.post(inventories, data=new_inv_1, expect=201, auth=self.get_super_credentials()) data = self.post(inventories, data=new_inv_1, expect=201, auth=self.get_super_credentials())
self.assertEquals(data['id'], 3) self.assertEquals(data['id'], new_id)
# an org admin of any org can create inventory, if it is one of his organizations # an org admin of any org can create inventory, if it is one of his organizations
# the organization parameter is required! # the organization parameter is required!
new_inv_incomplete = dict(name='inventory-d', description='baz') new_inv_incomplete = dict(name='inventory-d', description='baz')
data = self.post(inventories, data=new_inv_incomplete, expect=400, auth=self.get_normal_credentials()) data = self.post(inventories, data=new_inv_incomplete, expect=400, auth=self.get_normal_credentials())
new_inv_not_my_org = dict(name='inventory-d', description='baz', organization=3) new_inv_not_my_org = dict(name='inventory-d', description='baz', organization=self.organizations[2].pk)
data = self.post(inventories, data=new_inv_not_my_org, expect=403, auth=self.get_normal_credentials()) data = self.post(inventories, data=new_inv_not_my_org, expect=403, auth=self.get_normal_credentials())
new_inv_my_org = dict(name='inventory-d', description='baz', organization=1) new_inv_my_org = dict(name='inventory-d', description='baz', organization=self.organizations[0].pk)
data = self.post(inventories, data=new_inv_my_org, expect=201, auth=self.get_normal_credentials()) data = self.post(inventories, data=new_inv_my_org, expect=201, auth=self.get_normal_credentials())
# a regular user cannot create inventory # a regular user cannot create inventory
new_inv_denied = dict(name='inventory-e', description='glorp', organization=1) new_inv_denied = dict(name='inventory-e', description='glorp', organization=self.organizations[0].pk)
data = self.post(inventories, data=new_inv_denied, expect=403, auth=self.get_other_credentials()) data = self.post(inventories, data=new_inv_denied, expect=403, auth=self.get_other_credentials())
# a super user can add hosts (but inventory ID is required) # a super user can add hosts (but inventory ID is required)
@@ -146,18 +145,18 @@ class InventoryTest(BaseTest):
new_group_c = dict(name='web4', inventory=inv.pk) new_group_c = dict(name='web4', inventory=inv.pk)
new_group_d = dict(name='web5', inventory=inv.pk) new_group_d = dict(name='web5', inventory=inv.pk)
new_group_e = dict(name='web6', inventory=inv.pk) new_group_e = dict(name='web6', inventory=inv.pk)
groups = '/api/v1/groups/' groups = reverse('main:group_list')
data0 = self.post(groups, data=invalid, expect=400, auth=self.get_super_credentials()) data0 = self.post(groups, data=invalid, expect=400, auth=self.get_super_credentials())
data0 = self.post(groups, data=new_group_a, expect=201, auth=self.get_super_credentials()) data0 = self.post(groups, data=new_group_a, expect=201, auth=self.get_super_credentials())
# an org admin can add hosts # an org admin can add groups
group_data1 = self.post(groups, data=new_group_e, expect=201, auth=self.get_normal_credentials()) group_data1 = self.post(groups, data=new_group_e, expect=201, auth=self.get_normal_credentials())
# a normal user cannot add hosts # a normal user cannot add groups
group_data2 = self.post(groups, data=new_group_b, expect=403, auth=self.get_nobody_credentials()) group_data2 = self.post(groups, data=new_group_b, expect=403, auth=self.get_nobody_credentials())
# a normal user with inventory edit permissions (on any inventory) can create hosts # a normal user with inventory edit permissions (on any inventory) can create groups
# already done! # already done!
#edit_perm = Permission.objects.create( #edit_perm = Permission.objects.create(
# user = self.other_django_user, # user = self.other_django_user,
@@ -172,7 +171,7 @@ class InventoryTest(BaseTest):
################################################# #################################################
# HOSTS->inventories POST via subcollection # HOSTS->inventories POST via subcollection
url = '/api/v1/inventories/1/hosts/' url = reverse('main:inventory_hosts_list', args=(self.inventory_a.pk,))
new_host_a = dict(name='web100.example.com') new_host_a = dict(name='web100.example.com')
new_host_b = dict(name='web101.example.com') new_host_b = dict(name='web101.example.com')
new_host_c = dict(name='web102.example.com') new_host_c = dict(name='web102.example.com')
@@ -189,7 +188,7 @@ class InventoryTest(BaseTest):
added_by_collection_c = self.post(url, data=new_host_c, expect=403, auth=self.get_nobody_credentials()) added_by_collection_c = self.post(url, data=new_host_c, expect=403, auth=self.get_nobody_credentials())
# a normal user with edit permission on the inventory can associate hosts with inventories # a normal user with edit permission on the inventory can associate hosts with inventories
url5 = '/api/v1/inventories/5/hosts/' url5 = reverse('main:inventory_hosts_list', args=(inv.pk,))
added_by_collection_d = self.post(url5, data=new_host_d, expect=201, auth=self.get_other_credentials()) added_by_collection_d = self.post(url5, data=new_host_d, expect=201, auth=self.get_other_credentials())
got = self.get(url5, expect=200, auth=self.get_other_credentials()) got = self.get(url5, expect=200, auth=self.get_other_credentials())
self.assertEquals(got['count'], 4) self.assertEquals(got['count'], 4)
@@ -204,9 +203,9 @@ class InventoryTest(BaseTest):
################################################## ##################################################
# GROUPS->inventories POST via subcollection # GROUPS->inventories POST via subcollection
root_groups = '/api/v1/inventories/1/root_groups/' root_groups = reverse('main:inventory_root_groups_list', args=(self.inventory_a.pk,))
url = '/api/v1/inventories/1/groups/' url = reverse('main:inventory_groups_list', args=(self.inventory_a.pk,))
new_group_a = dict(name='web100') new_group_a = dict(name='web100')
new_group_b = dict(name='web101') new_group_b = dict(name='web101')
new_group_c = dict(name='web102') new_group_c = dict(name='web102')
@@ -223,7 +222,7 @@ class InventoryTest(BaseTest):
added_by_collection = self.post(url, data=new_group_c, expect=403, auth=self.get_nobody_credentials()) added_by_collection = self.post(url, data=new_group_c, expect=403, auth=self.get_nobody_credentials())
# a normal user with edit permissions on the inventory can associate groups with inventories # a normal user with edit permissions on the inventory can associate groups with inventories
url5 = '/api/v1/inventories/5/groups/' url5 = reverse('main:inventory_groups_list', args=(inv.pk,))
added_by_collection = self.post(url5, data=new_group_d, expect=201, auth=self.get_other_credentials()) added_by_collection = self.post(url5, data=new_group_d, expect=201, auth=self.get_other_credentials())
# make sure duplicates give 400s # make sure duplicates give 400s
self.post(url5, data=new_group_d, expect=400, auth=self.get_other_credentials()) self.post(url5, data=new_group_d, expect=400, auth=self.get_other_credentials())
@@ -248,7 +247,8 @@ class InventoryTest(BaseTest):
vars_c = dict(asdf=5555, dog='mouse', cat='mogwai', unstructured=dict(a=[3,0,3],b=dict(z=2600))) vars_c = dict(asdf=5555, dog='mouse', cat='mogwai', unstructured=dict(a=[3,0,3],b=dict(z=2600)))
# attempting to get a variable object creates it, even though it does not already exist # attempting to get a variable object creates it, even though it does not already exist
vdata_url = "/api/v1/hosts/%s/variable_data/" % (added_by_collection_a['id']) vdata_url = reverse('main:host_variable_detail', args=(added_by_collection_a['id'],))
got = self.get(vdata_url, expect=200, auth=self.get_super_credentials()) got = self.get(vdata_url, expect=200, auth=self.get_super_credentials())
self.assertEquals(got, {}) self.assertEquals(got, {})
@@ -266,18 +266,11 @@ class InventoryTest(BaseTest):
# a normal user cannot edit variable objects # a normal user cannot edit variable objects
self.put(vdata_url, data=vars_a, expect=403, auth=self.get_nobody_credentials()) self.put(vdata_url, data=vars_a, expect=403, auth=self.get_nobody_credentials())
# a normal user with inventory write permissions can edit variable objects # a normal user with inventory write permissions can edit variable objects... FIXME
#vdata_url = "/api/v1/hosts/1/variable_data/" #vdata_url = "/api/v1/hosts/1/variable_data/"
#got = self.put(vdata_url, data=vars_b, expect=200, auth=self.get_normal_credentials()) #got = self.put(vdata_url, data=vars_b, expect=200, auth=self.get_normal_credentials())
#self.assertEquals(got, vars_b) #self.assertEquals(got, vars_b)
# this URL is not one end users will use, but is what you get back from a put
# as a result, it also needs to be access controlled and working. You will not
# be able to put to it.
#backend_url = '/api/v1/variable_data/1/'
#got = self.get(backend_url, expect=200, auth=self.get_normal_credentials())
#got = self.put(backend_url, data=dict(), expect=403, auth=self.get_super_credentials())
################################################### ###################################################
# VARIABLES -> GROUPS # VARIABLES -> GROUPS
@@ -285,9 +278,9 @@ class InventoryTest(BaseTest):
vars_b = dict(asdf=8888, dog='snoopy', cat='cheshire', unstructured=dict(a=[2,2,2],b=dict(x=3,y=4))) vars_b = dict(asdf=8888, dog='snoopy', cat='cheshire', unstructured=dict(a=[2,2,2],b=dict(x=3,y=4)))
vars_c = dict(asdf=9999, dog='pluto', cat='five', unstructured=dict(a=[3,3,3],b=dict(z=5))) vars_c = dict(asdf=9999, dog='pluto', cat='five', unstructured=dict(a=[3,3,3],b=dict(z=5)))
groups = Group.objects.all() groups = Group.objects.all()
vdata1_url = "/api/v1/groups/%s/variable_data/" % (groups[0].pk) vdata1_url = reverse('main:group_variable_detail', args=(groups[0].pk,))
vdata2_url = "/api/v1/groups/%s/variable_data/" % (groups[1].pk) vdata2_url = reverse('main:group_variable_detail', args=(groups[1].pk,))
# a super user can associate variable objects with groups # a super user can associate variable objects with groups
got = self.get(vdata1_url, expect=200, auth=self.get_super_credentials()) got = self.get(vdata1_url, expect=200, auth=self.get_super_credentials())
@@ -312,7 +305,7 @@ class InventoryTest(BaseTest):
vars_b = dict(asdf=2736, dog='benji', cat='garfield', unstructured=dict(a=[2,2,2],b=dict(x=3,y=4))) vars_b = dict(asdf=2736, dog='benji', cat='garfield', unstructured=dict(a=[2,2,2],b=dict(x=3,y=4)))
vars_c = dict(asdf=7692, dog='buck', cat='sylvester', unstructured=dict(a=[3,3,3],b=dict(z=5))) vars_c = dict(asdf=7692, dog='buck', cat='sylvester', unstructured=dict(a=[3,3,3],b=dict(z=5)))
vdata_url = "/api/v1/inventories/%s/variable_data/" % (self.inventory_a.pk) vdata_url = reverse('main:inventory_variable_detail', args=(self.inventory_a.pk,))
# a super user can associate variable objects with inventory # a super user can associate variable objects with inventory
got = self.get(vdata_url, expect=200, auth=self.get_super_credentials()) got = self.get(vdata_url, expect=200, auth=self.get_super_credentials())
@@ -333,40 +326,44 @@ class InventoryTest(BaseTest):
#################################################### ####################################################
# ADDING HOSTS TO GROUPS # ADDING HOSTS TO GROUPS
groups = Group.objects.all() groups = Group.objects.order_by('pk')
hosts = Host.objects.all() hosts = Host.objects.order_by('pk')
groups[0].hosts.add(Host.objects.get(pk=1)) host1 = hosts[0]
groups[0].hosts.add(Host.objects.get(pk=3)) host2 = hosts[1]
host3 = hosts[2]
groups[0].hosts.add(host1)
groups[0].hosts.add(host3)
groups[0].save() groups[0].save()
# access # access
url1 = '/api/v1/groups/1/hosts/' url1 = reverse('main:group_hosts_list', args=(groups[0].pk,))
data = self.get(url1, expect=200, auth=self.get_normal_credentials()) data = self.get(url1, expect=200, auth=self.get_normal_credentials())
self.assertEquals(data['count'], 2) self.assertEquals(data['count'], 2)
self.assertEquals(data['results'][0]['id'], 1) self.assertTrue(host1.pk in [x['id'] for x in data['results']])
self.assertEquals(data['results'][1]['id'], 3) self.assertTrue(host3.pk in [x['id'] for x in data['results']])
# addition # addition
got = self.get('/api/v1/hosts/2/', expect=200, auth=self.get_normal_credentials()) url = reverse('main:host_detail', args=(host2.pk,))
self.assertEquals(got['id'], 2) got = self.get(url, expect=200, auth=self.get_normal_credentials())
posted = self.post('/api/v1/groups/1/hosts/', data=got, expect=204, auth=self.get_normal_credentials()) self.assertEquals(got['id'], host2.pk)
posted = self.post(url1, data=got, expect=204, auth=self.get_normal_credentials())
data = self.get(url1, expect=200, auth=self.get_normal_credentials()) data = self.get(url1, expect=200, auth=self.get_normal_credentials())
self.assertEquals(data['count'], 3) self.assertEquals(data['count'], 3)
self.assertEquals(data['results'][1]['id'], 2) self.assertTrue(host2.pk in [x['id'] for x in data['results']])
# now add one new completely new host, to test creation+association in one go # now add one new completely new host, to test creation+association in one go
new_host = dict(inventory=got['inventory'], name='completelynewhost.example.com', description='...') new_host = dict(inventory=got['inventory'], name='completelynewhost.example.com', description='...')
posted = self.post('/api/v1/groups/1/hosts/', data=new_host, expect=201, auth=self.get_normal_credentials()) posted = self.post(url1, data=new_host, expect=201, auth=self.get_normal_credentials())
data = self.get(url1, expect=200, auth=self.get_normal_credentials()) data = self.get(url1, expect=200, auth=self.get_normal_credentials())
self.assertEquals(data['count'], 4) self.assertEquals(data['count'], 4)
# removal # removal
got['disassociate'] = 1 got['disassociate'] = 1
posted = self.post('/api/v1/groups/1/hosts/', data=got, expect=204, auth=self.get_normal_credentials()) posted = self.post(url1, data=got, expect=204, auth=self.get_normal_credentials())
data = self.get(url1, expect=200, auth=self.get_normal_credentials()) data = self.get(url1, expect=200, auth=self.get_normal_credentials())
self.assertEquals(data['count'], 3) self.assertEquals(data['count'], 3)
self.assertEquals(data['results'][1]['id'], 3) self.assertFalse(host2.pk in [x['id'] for x in data['results']])
#################################################### ####################################################
# SUBGROUPS # SUBGROUPS
@@ -374,7 +371,7 @@ class InventoryTest(BaseTest):
groups = Group.objects.all() groups = Group.objects.all()
# just some more groups for kicks # just some more groups for kicks
inv = Inventory.objects.get(pk=1) inv = Inventory.objects.get(pk=self.inventory_a.pk)
Group.objects.create(name='group-X1', inventory=inv) Group.objects.create(name='group-X1', inventory=inv)
Group.objects.create(name='group-X2', inventory=inv) Group.objects.create(name='group-X2', inventory=inv)
Group.objects.create(name='group-X3', inventory=inv) Group.objects.create(name='group-X3', inventory=inv)
@@ -388,20 +385,26 @@ class InventoryTest(BaseTest):
) )
# data used for testing listing all hosts that are transitive members of a group # data used for testing listing all hosts that are transitive members of a group
g2 = Group.objects.get(pk=2) g2 = Group.objects.get(name='web4')
nh = Host.objects.create(name='newhost.example.com', inventory=inv, created_by=User.objects.get(pk=1)) nh = Host.objects.create(name='newhost.example.com', inventory=inv,
created_by=self.super_django_user)
g2.hosts.add(nh) g2.hosts.add(nh)
g2.save() g2.save()
# a super user can set subgroups # a super user can set subgroups
subgroups_url = '/api/v1/groups/1/children/' subgroups_url = reverse('main:group_children_list',
child_url = '/api/v1/groups/2/' args=(Group.objects.get(name='web2').pk,))
subgroups_url2 = '/api/v1/groups/3/children/' child_url = reverse('main:group_detail',
subgroups_url3 = '/api/v1/groups/4/children/' args=(Group.objects.get(name='web4').pk,))
subgroups_url4 = '/api/v1/groups/5/children/' subgroups_url2 = reverse('main:group_children_list',
args=(Group.objects.get(name='web6').pk,))
subgroups_url3 = reverse('main:group_children_list',
args=(Group.objects.get(name='web100').pk,))
subgroups_url4 = reverse('main:group_children_list',
args=(Group.objects.get(name='web101').pk,))
got = self.get(child_url, expect=200, auth=self.get_super_credentials()) got = self.get(child_url, expect=200, auth=self.get_super_credentials())
self.post(subgroups_url, data=got, expect=204, auth=self.get_super_credentials()) self.post(subgroups_url, data=got, expect=204, auth=self.get_super_credentials())
kids = Group.objects.get(pk=1).children.all() kids = Group.objects.get(name='web2').children.all()
self.assertEqual(len(kids), 1) self.assertEqual(len(kids), 1)
checked = self.get(subgroups_url, expect=200, auth=self.get_super_credentials()) checked = self.get(subgroups_url, expect=200, auth=self.get_super_credentials())
self.assertEquals(checked['count'], 1) self.assertEquals(checked['count'], 1)
@@ -410,7 +413,7 @@ class InventoryTest(BaseTest):
posted = self.post(subgroups_url2, data=got, expect=204, auth=self.get_normal_credentials()) posted = self.post(subgroups_url2, data=got, expect=204, auth=self.get_normal_credentials())
# see if we can post a completely new subgroup # see if we can post a completely new subgroup
new_data = dict(inventory=5, name='completely new', description='blarg?') new_data = dict(inventory=inv.pk, name='completely new', description='blarg?')
kids = self.get(subgroups_url2, expect=200, auth=self.get_normal_credentials()) kids = self.get(subgroups_url2, expect=200, auth=self.get_normal_credentials())
self.assertEqual(kids['count'], 1) self.assertEqual(kids['count'], 1)
posted2 = self.post(subgroups_url2, data=new_data, expect=201, auth=self.get_normal_credentials()) posted2 = self.post(subgroups_url2, data=new_data, expect=201, auth=self.get_normal_credentials())
@@ -432,8 +435,9 @@ class InventoryTest(BaseTest):
# slight detour # slight detour
# can see all hosts under a group, even if it has subgroups # can see all hosts under a group, even if it has subgroups
# this URL is NOT postable # this URL is NOT postable
all_hosts = '/api/v1/groups/1/all_hosts/' all_hosts = reverse('main:group_all_hosts_list',
self.assertEqual(Group.objects.get(pk=1).hosts.count(), 3) args=(Group.objects.get(name='web2').pk,))
self.assertEqual(Group.objects.get(name='web2').hosts.count(), 3)
data = self.get(all_hosts, expect=200, auth=self.get_normal_credentials()) data = self.get(all_hosts, expect=200, auth=self.get_normal_credentials())
self.post(all_hosts, data=dict(id=123456, msg='spam'), expect=405, auth=self.get_normal_credentials()) self.post(all_hosts, data=dict(id=123456, msg='spam'), expect=405, auth=self.get_normal_credentials())
self.assertEquals(data['count'], 4) self.assertEquals(data['count'], 4)

View File

@@ -14,7 +14,7 @@ from ansibleworks.main.tests.base import BaseTest
class OrganizationsTest(BaseTest): class OrganizationsTest(BaseTest):
def collection(self): def collection(self):
return '/api/v1/organizations/' return reverse('main:organization_list')
def setUp(self): def setUp(self):
super(OrganizationsTest, self).setUp() super(OrganizationsTest, self).setUp()
@@ -205,7 +205,8 @@ class OrganizationsTest(BaseTest):
data2 = self.post(self.collection(), new_org, expect=400, auth=self.get_super_credentials()) data2 = self.post(self.collection(), new_org, expect=400, auth=self.get_super_credentials())
# look at what we got back from the post, make sure we added an org # look at what we got back from the post, make sure we added an org
self.assertTrue(data1['url'].endswith("/11/")) last_org = Organization.objects.order_by('-pk')[0]
self.assertTrue(data1['url'].endswith("/%d/" % last_org.pk))
def test_post_item_subobjects_projects(self): def test_post_item_subobjects_projects(self):
@@ -262,20 +263,20 @@ class OrganizationsTest(BaseTest):
def test_post_item_subobjects_users(self): def test_post_item_subobjects_users(self):
url = '/api/v1/organizations/2/users/' url = reverse('main:organization_users_list', args=(self.organizations[1].pk,))
users = self.get(url, expect=200, auth=self.get_normal_credentials()) users = self.get(url, expect=200, auth=self.get_normal_credentials())
self.assertEqual(users['count'], 1) self.assertEqual(users['count'], 1)
self.post(url, dict(id=2), expect=204, auth=self.get_normal_credentials()) self.post(url, dict(id=self.normal_django_user.pk), expect=204, auth=self.get_normal_credentials())
users = self.get(url, expect=200, auth=self.get_normal_credentials()) users = self.get(url, expect=200, auth=self.get_normal_credentials())
self.assertEqual(users['count'], 2) self.assertEqual(users['count'], 2)
self.post(url, dict(id=2, disassociate=True), expect=204, auth=self.get_normal_credentials()) self.post(url, dict(id=self.normal_django_user.pk, disassociate=True), expect=204, auth=self.get_normal_credentials())
users = self.get(url, expect=200, auth=self.get_normal_credentials()) users = self.get(url, expect=200, auth=self.get_normal_credentials())
self.assertEqual(users['count'], 1) self.assertEqual(users['count'], 1)
# post a completely new user to verify we can add users to the subcollection directly # post a completely new user to verify we can add users to the subcollection directly
new_user = dict(username='NewUser9000') new_user = dict(username='NewUser9000')
which_org = self.normal_django_user.admin_of_organizations.all()[0] which_org = self.normal_django_user.admin_of_organizations.all()[0]
url = '/api/v1/organizations/%s/users/' % (which_org.pk) url = reverse('main:organization_users_list', args=(which_org.pk,))
posted = self.post(url, new_user, expect=201, auth=self.get_normal_credentials()) posted = self.post(url, new_user, expect=201, auth=self.get_normal_credentials())
all_users = self.get(url, expect=200, auth=self.get_normal_credentials()) all_users = self.get(url, expect=200, auth=self.get_normal_credentials())
@@ -283,13 +284,13 @@ class OrganizationsTest(BaseTest):
def test_post_item_subobjects_admins(self): def test_post_item_subobjects_admins(self):
url = '/api/v1/organizations/2/admins/' url = reverse('main:organization_admins_list', args=(self.organizations[1].pk,))
admins = self.get(url, expect=200, auth=self.get_normal_credentials()) admins = self.get(url, expect=200, auth=self.get_normal_credentials())
self.assertEqual(admins['count'], 1) self.assertEqual(admins['count'], 1)
self.post(url, dict(id=1), expect=204, auth=self.get_normal_credentials()) self.post(url, dict(id=self.super_django_user.pk), expect=204, auth=self.get_normal_credentials())
admins = self.get(url, expect=200, auth=self.get_normal_credentials()) admins = self.get(url, expect=200, auth=self.get_normal_credentials())
self.assertEqual(admins['count'], 2) self.assertEqual(admins['count'], 2)
self.post(url, dict(id=1, disassociate=1), expect=204, auth=self.get_normal_credentials()) self.post(url, dict(id=self.super_django_user.pk, disassociate=1), expect=204, auth=self.get_normal_credentials())
admins = self.get(url, expect=200, auth=self.get_normal_credentials()) admins = self.get(url, expect=200, auth=self.get_normal_credentials())
self.assertEqual(admins['count'], 1) self.assertEqual(admins['count'], 1)

View File

@@ -25,7 +25,7 @@ class ProjectsTest(BaseTest):
# tests for users, projects, and teams # tests for users, projects, and teams
def collection(self): def collection(self):
return '/api/v1/projects/' return reverse('main:project_list')
def setUp(self): def setUp(self):
super(ProjectsTest, self).setUp() super(ProjectsTest, self).setUp()
@@ -171,7 +171,7 @@ class ProjectsTest(BaseTest):
# PROJECTS - LISTING # PROJECTS - LISTING
# can get projects list # can get projects list
projects = '/api/v1/projects/' projects = reverse('main:project_list')
# invalid auth # invalid auth
self.get(projects, expect=401) self.get(projects, expect=401)
self.get(projects, expect=401, auth=self.get_invalid_credentials()) self.get(projects, expect=401, auth=self.get_invalid_credentials())
@@ -190,7 +190,7 @@ class ProjectsTest(BaseTest):
# ===================================================================== # =====================================================================
# PROJECTS - ACCESS # PROJECTS - ACCESS
project = '/api/v1/projects/%s/' % self.projects[3].pk project = reverse('main:project_detail', args=(self.projects[3].pk,))
self.get(project, expect=200, auth=self.get_super_credentials()) self.get(project, expect=200, auth=self.get_super_credentials())
self.get(project, expect=200, auth=self.get_normal_credentials()) self.get(project, expect=200, auth=self.get_normal_credentials())
self.get(project, expect=403, auth=self.get_other_credentials()) self.get(project, expect=403, auth=self.get_other_credentials())
@@ -201,25 +201,25 @@ class ProjectsTest(BaseTest):
self.get(project, expect=404, auth=self.get_normal_credentials()) self.get(project, expect=404, auth=self.get_normal_credentials())
# can list playbooks for projects # can list playbooks for projects
proj_playbooks = '/api/v1/projects/%d/playbooks/' % self.projects[2].pk proj_playbooks = reverse('main:project_detail_playbooks', args=(self.projects[2].pk,))
got = self.get(proj_playbooks, expect=200, auth=self.get_super_credentials()) got = self.get(proj_playbooks, expect=200, auth=self.get_super_credentials())
self.assertEqual(got, self.projects[2].playbooks) self.assertEqual(got, self.projects[2].playbooks)
# can list member organizations for projects # can list member organizations for projects
proj_orgs = '/api/v1/projects/1/organizations/' proj_orgs = reverse('main:project_organizations_list', args=(self.projects[0].pk,))
# only usable as superuser # only usable as superuser
got = self.get(proj_orgs, expect=403, auth=self.get_normal_credentials()) got = self.get(proj_orgs, expect=403, auth=self.get_normal_credentials())
got = self.get(proj_orgs, expect=200, auth=self.get_super_credentials()) got = self.get(proj_orgs, expect=200, auth=self.get_super_credentials())
self.assertEquals(got['count'], 1) self.assertEquals(got['count'], 1)
self.assertEquals(got['results'][0]['url'], '/api/v1/organizations/1/') self.assertEquals(got['results'][0]['url'], reverse('main:organization_detail', args=(self.organizations[0].pk,)))
# you can't add organizations to projects here, verify that this is true (405) # you can't add organizations to projects here, verify that this is true (405)
self.post(proj_orgs, data={}, expect=405, auth=self.get_super_credentials()) self.post(proj_orgs, data={}, expect=405, auth=self.get_super_credentials())
# ===================================================================== # =====================================================================
# TEAMS # TEAMS
all_teams = '/api/v1/teams/' all_teams = reverse('main:team_list')
team1 = '/api/v1/teams/1/' team1 = reverse('main:team_detail', args=(self.team1.pk,))
# can list teams # can list teams
got = self.get(all_teams, expect=200, auth=self.get_super_credentials()) got = self.get(all_teams, expect=200, auth=self.get_super_credentials())
@@ -228,7 +228,7 @@ class ProjectsTest(BaseTest):
# can get teams # can get teams
got = self.get(team1, expect=200, auth=self.get_super_credentials()) got = self.get(team1, expect=200, auth=self.get_super_credentials())
self.assertEquals(got['url'], '/api/v1/teams/1/') self.assertEquals(got['url'], reverse('main:team_detail', args=(self.team1.pk,)))
got = self.get(team1, expect=200, auth=self.get_normal_credentials()) got = self.get(team1, expect=200, auth=self.get_normal_credentials())
got = self.get(team1, expect=403, auth=self.get_other_credentials()) got = self.get(team1, expect=403, auth=self.get_other_credentials())
self.team1.users.add(User.objects.get(username='other')) self.team1.users.add(User.objects.get(username='other'))
@@ -236,9 +236,9 @@ class ProjectsTest(BaseTest):
got = self.get(team1, expect=200, auth=self.get_other_credentials()) got = self.get(team1, expect=200, auth=self.get_other_credentials())
got = self.get(team1, expect=403, auth=self.get_nobody_credentials()) got = self.get(team1, expect=403, auth=self.get_nobody_credentials())
new_team = dict(name='newTeam', description='blarg', organization=1) new_team = dict(name='newTeam', description='blarg', organization=self.organizations[0].pk)
new_team2 = dict(name='newTeam2', description='blarg', organization=1) new_team2 = dict(name='newTeam2', description='blarg', organization=self.organizations[0].pk)
new_team3 = dict(name='newTeam3', description='bad wolf', organization=1) new_team3 = dict(name='newTeam3', description='bad wolf', organization=self.organizations[0].pk)
# can add teams # can add teams
posted1 = self.post(all_teams, data=new_team, expect=201, auth=self.get_super_credentials()) posted1 = self.post(all_teams, data=new_team, expect=201, auth=self.get_super_credentials())
@@ -250,8 +250,8 @@ class ProjectsTest(BaseTest):
url3 = posted3['url'] url3 = posted3['url']
url5 = posted1['url'] url5 = posted1['url']
new_team = Team.objects.create(name='newTeam4', organization=Organization.objects.get(pk=2)) new_team = Team.objects.create(name='newTeam4', organization=self.organizations[1])
url = '/api/v1/teams/%s/' % new_team.pk url = reverse('main:team_detail', args=(new_team.pk,))
# can delete teams # can delete teams
self.delete(url, expect=401) self.delete(url, expect=401)
@@ -264,7 +264,7 @@ class ProjectsTest(BaseTest):
# ORGANIZATION TEAMS # ORGANIZATION TEAMS
# can list organization teams (filtered by user) -- this is an org admin function # can list organization teams (filtered by user) -- this is an org admin function
org_teams = '/api/v1/organizations/2/teams/' org_teams = reverse('main:organization_teams_list', args=(self.organizations[1].pk,))
data1 = self.get(org_teams, expect=401) data1 = self.get(org_teams, expect=401)
data2 = self.get(org_teams, expect=403, auth=self.get_nobody_credentials()) data2 = self.get(org_teams, expect=403, auth=self.get_nobody_credentials())
data3 = self.get(org_teams, expect=403, auth=self.get_other_credentials()) data3 = self.get(org_teams, expect=403, auth=self.get_other_credentials())
@@ -293,8 +293,8 @@ class ProjectsTest(BaseTest):
# ===================================================================== # =====================================================================
# TEAM PROJECTS # TEAM PROJECTS
team = Team.objects.filter(organization__pk = 2)[0] team = Team.objects.filter(organization__pk=self.organizations[1].pk)[0]
team_projects = '/api/v1/teams/%s/projects/' % (team.pk) team_projects = reverse('main:team_projects_list', args=(team.pk,))
p1 = self.projects[0] p1 = self.projects[0]
team.projects.add(p1) team.projects.add(p1)
@@ -310,8 +310,8 @@ class ProjectsTest(BaseTest):
# ===================================================================== # =====================================================================
# TEAMS USER MEMBERSHIP # TEAMS USER MEMBERSHIP
team = Team.objects.filter(organization__pk = 2)[0] team = Team.objects.filter(organization__pk=self.organizations[1].pk)[0]
team_users = '/api/v1/teams/%s/users/' % (team.pk) team_users = reverse('main:team_users_list', args=(team.pk,))
for x in team.users.all(): for x in team.users.all():
team.users.remove(x) team.users.remove(x)
team.save() team.save()
@@ -325,7 +325,7 @@ class ProjectsTest(BaseTest):
self.get(team_users, expect=200, auth=self.get_super_credentials()) self.get(team_users, expect=200, auth=self.get_super_credentials())
# can add users to teams # can add users to teams
all_users = self.get('/api/v1/users/', expect=200, auth=self.get_super_credentials()) all_users = self.get(reverse('main:user_list'), expect=200, auth=self.get_super_credentials())
for x in all_users['results']: for x in all_users['results']:
self.post(team_users, data=x, expect=403, auth=self.get_nobody_credentials()) self.post(team_users, data=x, expect=403, auth=self.get_nobody_credentials())
self.post(team_users, data=x, expect=204, auth=self.get_normal_credentials()) self.post(team_users, data=x, expect=204, auth=self.get_normal_credentials())
@@ -345,11 +345,11 @@ class ProjectsTest(BaseTest):
# from a user, can see what teams they are on (related resource) # from a user, can see what teams they are on (related resource)
other = User.objects.get(username = 'other') other = User.objects.get(username = 'other')
url = '/api/v1/users/%s/teams/' % other.pk url = reverse('main:user_teams_list', args=(other.pk,))
self.get(url, expect=401) self.get(url, expect=401)
self.get(url, expect=401, auth=self.get_invalid_credentials()) self.get(url, expect=401, auth=self.get_invalid_credentials())
self.get(url, expect=403, auth=self.get_nobody_credentials()) self.get(url, expect=403, auth=self.get_nobody_credentials())
other.organizations.add(Organization.objects.get(pk=2)) other.organizations.add(Organization.objects.get(pk=self.organizations[1].pk))
other.save() other.save()
my_teams1 = self.get(url, expect=200, auth=self.get_normal_credentials()) my_teams1 = self.get(url, expect=200, auth=self.get_normal_credentials())
my_teams2 = self.get(url, expect=200, auth=self.get_other_credentials()) my_teams2 = self.get(url, expect=200, auth=self.get_other_credentials())
@@ -359,7 +359,7 @@ class ProjectsTest(BaseTest):
# ===================================================================== # =====================================================================
# USER PROJECTS # USER PROJECTS
url = '/api/v1/users/%s/projects/' % other.pk url = reverse('main:user_projects_list', args=(other.pk,))
# from a user, can see what projects they can see based on team association # from a user, can see what projects they can see based on team association
# though this resource doesn't do anything else # though this resource doesn't do anything else
@@ -373,12 +373,12 @@ class ProjectsTest(BaseTest):
# ===================================================================== # =====================================================================
# CREDENTIALS # CREDENTIALS
other_creds = '/api/v1/users/%s/credentials/' % other.pk other_creds = reverse('main:user_credentials_list', args=(other.pk,))
team_creds = '/api/v1/teams/%s/credentials/' % team.pk team_creds = reverse('main:team_credentials_list', args=(team.pk,))
new_credentials = dict( new_credentials = dict(
name = 'credential', name = 'credential',
project = Project.objects.all()[0].pk, project = Project.objects.order_by('pk')[0].pk,
default_username = 'foo', default_username = 'foo',
ssh_key_data = 'bar', ssh_key_data = 'bar',
ssh_key_unlock = 'baz', ssh_key_unlock = 'baz',
@@ -441,11 +441,11 @@ class ProjectsTest(BaseTest):
# can edit a credential # can edit a credential
cred_user = Credential.objects.get(pk=cred_user) cred_user = Credential.objects.get(pk=cred_user)
cred_team = Credential.objects.get(pk=cred_team) cred_team = Credential.objects.get(pk=cred_team)
d_cred_user = dict(id=cred_user.pk, name='x', sudo_password='blippy', user=cred_user.pk) d_cred_user = dict(id=cred_user.pk, name='x', sudo_password='blippy', user=cred_user.user.pk)
d_cred_user2 = dict(id=cred_user.pk, name='x', sudo_password='blippy', user=User.objects.get(pk=1).pk) d_cred_user2 = dict(id=cred_user.pk, name='x', sudo_password='blippy', user=self.super_django_user.pk)
d_cred_team = dict(id=cred_team.pk, name='x', sudo_password='blippy', team=cred_team.pk) d_cred_team = dict(id=cred_team.pk, name='x', sudo_password='blippy', team=cred_team.team.pk)
edit_creds1 = '/api/v1/credentials/%s/' % cred_user.pk edit_creds1 = reverse('main:credential_detail', args=(cred_user.pk,))
edit_creds2 = '/api/v1/credentials/%s/' % cred_team.pk edit_creds2 = reverse('main:credential_detail', args=(cred_team.pk,))
self.put(edit_creds1, data=d_cred_user, expect=401) self.put(edit_creds1, data=d_cred_user, expect=401)
self.put(edit_creds1, data=d_cred_user, expect=401, auth=self.get_invalid_credentials()) self.put(edit_creds1, data=d_cred_user, expect=401, auth=self.get_invalid_credentials())
@@ -458,20 +458,18 @@ class ProjectsTest(BaseTest):
self.put(edit_creds2, data=d_cred_team, expect=401) self.put(edit_creds2, data=d_cred_team, expect=401)
self.put(edit_creds2, data=d_cred_team, expect=401, auth=self.get_invalid_credentials()) self.put(edit_creds2, data=d_cred_team, expect=401, auth=self.get_invalid_credentials())
cred_team = Credential.objects.get(pk=cred_team.pk)
self.put(edit_creds2, data=d_cred_team, expect=200, auth=self.get_super_credentials()) self.put(edit_creds2, data=d_cred_team, expect=200, auth=self.get_super_credentials())
cred_team = Credential.objects.get(pk=cred_team.pk)
cred_put_t = self.put(edit_creds2, data=d_cred_team, expect=200, auth=self.get_normal_credentials()) cred_put_t = self.put(edit_creds2, data=d_cred_team, expect=200, auth=self.get_normal_credentials())
self.put(edit_creds2, data=d_cred_team, expect=403, auth=self.get_other_credentials()) self.put(edit_creds2, data=d_cred_team, expect=403, auth=self.get_other_credentials())
cred_put_t['disassociate'] = 1 cred_put_t['disassociate'] = 1
team_url = "/api/v1/teams/%s/credentials/" % cred_put_t['team'] team_url = reverse('main:team_credentials_list', args=(cred_put_t['team'],))
self.post(team_url, data=cred_put_t, expect=204, auth=self.get_normal_credentials()) self.post(team_url, data=cred_put_t, expect=204, auth=self.get_normal_credentials())
# can remove credentials from a user (via disassociate) # can remove credentials from a user (via disassociate)
cred_put_u['disassociate'] = 1 cred_put_u['disassociate'] = 1
url = cred_put_u['url'] url = cred_put_u['url']
user_url = "/api/v1/users/%s/credentials/" % cred_put_u['user'] user_url = reverse('main:user_credentials_list', args=(cred_put_u['user'],))
self.post(user_url, data=cred_put_u, expect=204, auth=self.get_normal_credentials()) self.post(user_url, data=cred_put_u, expect=204, auth=self.get_normal_credentials())
# can delete a credential directly -- probably won't be used too often # can delete a credential directly -- probably won't be used too often
@@ -482,14 +480,14 @@ class ProjectsTest(BaseTest):
# PERMISSIONS # PERMISSIONS
user = self.other_django_user user = self.other_django_user
team = Team.objects.get(pk=1) team = Team.objects.order_by('pk')[0]
organization = Organization.objects.get(pk=1) organization = Organization.objects.order_by('pk')[0]
inventory = Inventory.objects.create( inventory = Inventory.objects.create(
name = 'test inventory', name = 'test inventory',
organization = organization, organization = organization,
created_by = self.super_django_user created_by = self.super_django_user
) )
project = Project.objects.get(pk=1) project = Project.objects.order_by('pk')[0]
# can add permissions to a user # can add permissions to a user
@@ -508,26 +506,26 @@ class ProjectsTest(BaseTest):
permission_type=PERM_INVENTORY_DEPLOY permission_type=PERM_INVENTORY_DEPLOY
) )
url = '/api/v1/users/%s/permissions/' % user.pk url = reverse('main:user_permissions_list', args=(user.pk,))
posted = self.post(url, user_permission, expect=201, auth=self.get_super_credentials()) posted = self.post(url, user_permission, expect=201, auth=self.get_super_credentials())
url2 = posted['url'] url2 = posted['url']
got = self.get(url2, expect=200, auth=self.get_other_credentials()) got = self.get(url2, expect=200, auth=self.get_other_credentials())
# can add permissions on a team # can add permissions on a team
url = '/api/v1/teams/%s/permissions/' % team.pk url = reverse('main:team_permissions_list', args=(team.pk,))
posted = self.post(url, team_permission, expect=201, auth=self.get_super_credentials()) posted = self.post(url, team_permission, expect=201, auth=self.get_super_credentials())
url2 = posted['url'] url2 = posted['url']
# check we can get that permission back # check we can get that permission back
got = self.get(url2, expect=200, auth=self.get_other_credentials()) got = self.get(url2, expect=200, auth=self.get_other_credentials())
# can list permissions on a user # can list permissions on a user
url = '/api/v1/users/%s/permissions/' % user.pk url = reverse('main:user_permissions_list', args=(user.pk,))
got = self.get(url, expect=200, auth=self.get_super_credentials()) got = self.get(url, expect=200, auth=self.get_super_credentials())
got = self.get(url, expect=200, auth=self.get_other_credentials()) got = self.get(url, expect=200, auth=self.get_other_credentials())
got = self.get(url, expect=403, auth=self.get_nobody_credentials()) got = self.get(url, expect=403, auth=self.get_nobody_credentials())
# can list permissions on a team # can list permissions on a team
url = '/api/v1/teams/%s/permissions/' % team.pk url = reverse('main:team_permissions_list', args=(team.pk,))
got = self.get(url, expect=200, auth=self.get_super_credentials()) got = self.get(url, expect=200, auth=self.get_super_credentials())
got = self.get(url, expect=200, auth=self.get_other_credentials()) got = self.get(url, expect=200, auth=self.get_other_credentials())
got = self.get(url, expect=403, auth=self.get_nobody_credentials()) got = self.get(url, expect=403, auth=self.get_nobody_credentials())
@@ -542,12 +540,3 @@ class ProjectsTest(BaseTest):
self.delete(url2, expect=403, auth=self.get_other_credentials()) self.delete(url2, expect=403, auth=self.get_other_credentials())
self.delete(url2, expect=204, auth=self.get_super_credentials()) self.delete(url2, expect=204, auth=self.get_super_credentials())
self.delete(url2, expect=404, auth=self.get_other_credentials()) self.delete(url2, expect=404, auth=self.get_other_credentials())

View File

@@ -6,13 +6,15 @@ import json
from django.contrib.auth.models import User as DjangoUser from django.contrib.auth.models import User as DjangoUser
import django.test import django.test
from django.test.client import Client from django.test.client import Client
from django.core.urlresolvers import reverse
from ansibleworks.main.models import * from ansibleworks.main.models import *
from ansibleworks.main.tests.base import BaseTest from ansibleworks.main.tests.base import BaseTest
class UsersTest(BaseTest): class UsersTest(BaseTest):
def collection(self): def collection(self):
return '/api/v1/users/' return reverse('main:user_list')
def setUp(self): def setUp(self):
super(UsersTest, self).setUp() super(UsersTest, self).setUp()
@@ -23,7 +25,7 @@ class UsersTest(BaseTest):
self.organizations[0].users.add(self.normal_django_user) self.organizations[0].users.add(self.normal_django_user)
def test_only_super_user_or_org_admin_can_add_users(self): def test_only_super_user_or_org_admin_can_add_users(self):
url = '/api/v1/users/' url = reverse('main:user_list')
new_user = dict(username='blippy') new_user = dict(username='blippy')
new_user2 = dict(username='blippy2') new_user2 = dict(username='blippy2')
self.post(url, expect=401, data=new_user, auth=None) self.post(url, expect=401, data=new_user, auth=None)
@@ -35,7 +37,7 @@ class UsersTest(BaseTest):
self.post(url, expect=400, data=new_user2, auth=self.get_normal_credentials()) self.post(url, expect=400, data=new_user2, auth=self.get_normal_credentials())
def test_auth_token_login(self): def test_auth_token_login(self):
auth_token_url = '/api/v1/authtoken/' auth_token_url = reverse('main:auth_token_view')
# Always returns a 405 for any GET request, regardless of credentials. # Always returns a 405 for any GET request, regardless of credentials.
self.get(auth_token_url, expect=405, auth=None) self.get(auth_token_url, expect=405, auth=None)
@@ -57,13 +59,13 @@ class UsersTest(BaseTest):
auth_token = result['token'] auth_token = result['token']
# Verify we can access our own user information with the auth token. # Verify we can access our own user information with the auth token.
data = self.get('/api/v1/me/', expect=200, auth=auth_token) data = self.get(reverse('main:user_me_list'), expect=200, auth=auth_token)
self.assertEquals(data['results'][0]['username'], 'normal') self.assertEquals(data['results'][0]['username'], 'normal')
self.assertEquals(data['count'], 1) self.assertEquals(data['count'], 1)
def test_ordinary_user_can_modify_some_fields_about_himself_but_not_all_and_passwords_work(self): def test_ordinary_user_can_modify_some_fields_about_himself_but_not_all_and_passwords_work(self):
detail_url = '/api/v1/users/%s/' % self.other_django_user.pk detail_url = reverse('main:user_detail', args=(self.other_django_user.pk,))
data = self.get(detail_url, expect=200, auth=self.get_other_credentials()) data = self.get(detail_url, expect=200, auth=self.get_other_credentials())
# can't change first_name, last_name, etc # can't change first_name, last_name, etc
@@ -105,7 +107,7 @@ class UsersTest(BaseTest):
def test_user_created_with_password_can_login(self): def test_user_created_with_password_can_login(self):
# this is something an org admin can do... # this is something an org admin can do...
url = '/api/v1/users/' url = reverse('main:user_list')
data = dict(username='username', password='password') data = dict(username='username', password='password')
data2 = dict(username='username2', password='password2') data2 = dict(username='username2', password='password2')
data = self.post(url, expect=201, data=data, auth=self.get_normal_credentials()) data = self.post(url, expect=201, data=data, auth=self.get_normal_credentials())
@@ -123,22 +125,22 @@ class UsersTest(BaseTest):
self.get(url, expect=200, auth=('username2', 'password2')) self.get(url, expect=200, auth=('username2', 'password2'))
# verify that if you post a user with a pk, you do not alter that user's password info # verify that if you post a user with a pk, you do not alter that user's password info
mod = dict(id=1, username='change', password='change') mod = dict(id=self.super_django_user.pk, username='change', password='change')
data = self.post(url, expect=201, data=mod, auth=self.get_super_credentials()) data = self.post(url, expect=201, data=mod, auth=self.get_super_credentials())
orig = User.objects.get(pk=1) orig = User.objects.get(pk=self.super_django_user.pk)
self.assertTrue(orig.username != 'change') self.assertTrue(orig.username != 'change')
def test_password_not_shown_in_get_operations_for_list_or_detail(self): def test_password_not_shown_in_get_operations_for_list_or_detail(self):
url = '/api/v1/users/1/' url = reverse('main:user_detail', args=(self.super_django_user.pk,))
data = self.get(url, expect=200, auth=self.get_super_credentials()) data = self.get(url, expect=200, auth=self.get_super_credentials())
self.assertTrue('password' not in data) self.assertTrue('password' not in data)
url = '/api/v1/users/' url = reverse('main:user_list')
data = self.get(url, expect=200, auth=self.get_super_credentials()) data = self.get(url, expect=200, auth=self.get_super_credentials())
self.assertTrue('password' not in data['results'][0]) self.assertTrue('password' not in data['results'][0])
def test_user_list_filtered(self): def test_user_list_filtered(self):
url = '/api/v1/users/' url = reverse('main:user_list')
data3 = self.get(url, expect=200, auth=self.get_super_credentials()) data3 = self.get(url, expect=200, auth=self.get_super_credentials())
self.assertEquals(data3['count'], 3) self.assertEquals(data3['count'], 3)
data2 = self.get(url, expect=200, auth=self.get_normal_credentials()) data2 = self.get(url, expect=200, auth=self.get_normal_credentials())
@@ -147,23 +149,23 @@ class UsersTest(BaseTest):
self.assertEquals(data1['count'], 1) self.assertEquals(data1['count'], 1)
def test_super_user_can_delete_a_user_but_only_marked_inactive(self): def test_super_user_can_delete_a_user_but_only_marked_inactive(self):
url = '/api/v1/users/2/' user_pk = self.normal_django_user.pk
url = reverse('main:user_detail', args=(user_pk,))
data = self.delete(url, expect=204, auth=self.get_super_credentials()) data = self.delete(url, expect=204, auth=self.get_super_credentials())
data = self.get(url, expect=404, auth=self.get_super_credentials()) data = self.get(url, expect=404, auth=self.get_super_credentials())
url = '/api/v1/users/2/' obj = User.objects.get(pk=user_pk)
obj = User.objects.get(pk=2)
self.assertEquals(obj.is_active, False) self.assertEquals(obj.is_active, False)
def test_non_org_admin_user_cannot_delete_any_user_including_himself(self): def test_non_org_admin_user_cannot_delete_any_user_including_himself(self):
url1 = '/api/v1/users/1/' url1 = reverse('main:user_detail', args=(self.super_django_user.pk,))
url2 = '/api/v1/users/2/' url2 = reverse('main:user_detail', args=(self.normal_django_user.pk,))
url3 = '/api/v1/users/3/' url3 = reverse('main:user_detail', args=(self.other_django_user.pk,))
data = self.delete(url1, expect=403, auth=self.get_other_credentials()) data = self.delete(url1, expect=403, auth=self.get_other_credentials())
data = self.delete(url2, expect=403, auth=self.get_other_credentials()) data = self.delete(url2, expect=403, auth=self.get_other_credentials())
data = self.delete(url3, expect=403, auth=self.get_other_credentials()) data = self.delete(url3, expect=403, auth=self.get_other_credentials())
def test_there_exists_an_obvious_url_where_a_user_may_find_his_user_record(self): def test_there_exists_an_obvious_url_where_a_user_may_find_his_user_record(self):
url = '/api/v1/me/' url = reverse('main:user_me_list')
data = self.get(url, expect=401, auth=None) data = self.get(url, expect=401, auth=None)
data = self.get(url, expect=401, auth=self.get_invalid_credentials()) data = self.get(url, expect=401, auth=self.get_invalid_credentials())
data = self.get(url, expect=200, auth=self.get_normal_credentials()) data = self.get(url, expect=200, auth=self.get_normal_credentials())
@@ -179,7 +181,8 @@ class UsersTest(BaseTest):
def test_user_related_resources(self): def test_user_related_resources(self):
# organizations the user is a member of, should be 1 # organizations the user is a member of, should be 1
url = '/api/v1/users/2/organizations/' url = reverse('main:user_organizations_list',
args=(self.normal_django_user.pk,))
data = self.get(url, expect=200, auth=self.get_normal_credentials()) data = self.get(url, expect=200, auth=self.get_normal_credentials())
self.assertEquals(data['count'], 1) self.assertEquals(data['count'], 1)
# also accessible via superuser # also accessible via superuser
@@ -189,7 +192,8 @@ class UsersTest(BaseTest):
data = self.get(url, expect=403, auth=self.get_other_credentials()) data = self.get(url, expect=403, auth=self.get_other_credentials())
# organizations the user is an admin of, should be 1 # organizations the user is an admin of, should be 1
url = '/api/v1/users/2/admin_of_organizations/' url = reverse('main:user_admin_of_organizations_list',
args=(self.normal_django_user.pk,))
data = self.get(url, expect=200, auth=self.get_normal_credentials()) data = self.get(url, expect=200, auth=self.get_normal_credentials())
self.assertEquals(data['count'], 1) self.assertEquals(data['count'], 1)
# also accessible via superuser # also accessible via superuser
@@ -199,7 +203,7 @@ class UsersTest(BaseTest):
data = self.get(url, expect=403, auth=self.get_other_credentials()) data = self.get(url, expect=403, auth=self.get_other_credentials())
# teams the user is on, should be 0 # teams the user is on, should be 0
url = '/api/v1/users/2/teams/' url = reverse('main:user_teams_list', args=(self.normal_django_user.pk,))
data = self.get(url, expect=200, auth=self.get_normal_credentials()) data = self.get(url, expect=200, auth=self.get_normal_credentials())
self.assertEquals(data['count'], 0) self.assertEquals(data['count'], 0)
# also accessible via superuser # also accessible via superuser
@@ -209,13 +213,16 @@ class UsersTest(BaseTest):
data = self.get(url, expect=403, auth=self.get_other_credentials()) data = self.get(url, expect=403, auth=self.get_other_credentials())
# verify org admin can still read other user data too # verify org admin can still read other user data too
url = '/api/v1/users/3/organizations/' url = reverse('main:user_organizations_list',
args=(self.other_django_user.pk,))
data = self.get(url, expect=200, auth=self.get_normal_credentials()) data = self.get(url, expect=200, auth=self.get_normal_credentials())
self.assertEquals(data['count'], 1) self.assertEquals(data['count'], 1)
url = '/api/v1/users/3/admin_of_organizations/' url = reverse('main:user_admin_of_organizations_list',
args=(self.other_django_user.pk,))
data = self.get(url, expect=200, auth=self.get_normal_credentials()) data = self.get(url, expect=200, auth=self.get_normal_credentials())
self.assertEquals(data['count'], 0) self.assertEquals(data['count'], 0)
url = '/api/v1/users/3/teams/' url = reverse('main:user_teams_list',
args=(self.other_django_user.pk,))
data = self.get(url, expect=200, auth=self.get_normal_credentials()) data = self.get(url, expect=200, auth=self.get_normal_credentials())
self.assertEquals(data['count'], 0) self.assertEquals(data['count'], 0)