def test_model_item_status(self): "Test item status model" obj = ItemStatus(name='test') obj.save() self.assertEquals('test', obj.name) self.assertNotEquals(obj.id, None) obj.delete()
def test_model_item(self): "Test item model" type = ItemType(name='test') type.save() status = ItemStatus(name='test') status.save() obj = Item(name='test', item_type=type, status=status) obj.save() self.assertEquals('test', obj.name) self.assertNotEquals(obj.id, None) obj.delete()
def test_model_item_value(self): "Test item value model" status = ItemStatus(name='test') status.save() type = ItemType(name='test') type.save() item = Item(name='test', item_type=type, status=status) item.save() field = ItemField(name='test', label='test', field_type='text') field.save() obj = ItemValue(value='test', field=field, item=item) obj.save() self.assertEquals('test', obj.value) self.assertNotEquals(obj.id, None) obj.delete()
class InfrastructureApiTest(TestCase): "Infrastructure functional tests for api" username = "******" password = "******" prepared = False authentication_headers ={"CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION" : "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk" } content_type ='application/json' def setUp(self): "Initial Setup" if not self.prepared: # Clean up first Object.objects.all().delete() # Create objects try: self.group = Group.objects.get(name='test') except Group.DoesNotExist: Group.objects.all().delete() self.group = Group(name='test') self.group.save() try: self.user = DjangoUser.objects.get(username=self.username) self.user.set_password(self.password) try: self.profile = self.user.get_profile() except Exception: User.objects.all().delete() self.user = DjangoUser(username=self.username, password='') self.user.set_password(self.password) self.user.save() except DjangoUser.DoesNotExist: User.objects.all().delete() self.user = DjangoUser(username=self.username, password='') self.user.set_password(self.password) self.user.save() try: perspective = Perspective.objects.get(name='default') except Perspective.DoesNotExist: Perspective.objects.all().delete() perspective = Perspective(name='default') perspective.set_default_user() perspective.save() ModuleSetting.set('default_perspective', perspective.id) self.field = ItemField(name='test', label='test', field_type='text') self.field.set_default_user() self.field.save() self.type = ItemType(name='test') self.type.set_default_user() self.type.save() self.type.fields.add(self.field) self.status = ItemStatus(name='test') self.status.set_default_user() self.status.save() self.item = Item(name='test', item_type=self.type, status=self.status) self.item.set_default_user() self.item.save() self.value = ItemValue(field=self.field, item=self.item) self.value.save() self.servicing = ItemServicing(name='test') self.servicing.set_default_user() self.servicing.save() self.client = Client() self.prepared = True def test_unauthenticated_access(self): "Test index page at /api/infrastructure/types" response = self.client.get('/api/infrastructure/types') # Redirects as unauthenticated self.assertEquals(response.status_code, 401) def test_get_fields_list(self): """ Test index page api/infrastructure/types """ response = self.client.get(path=reverse('api_infrastructure_fields'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_field(self): response = self.client.get(path=reverse('api_infrastructure_fields', kwargs={'object_ptr': self.field.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_field(self): updates = {"name": "Api_name", "label": "Api label", "field_type": "text", "required": True, "details": "Api details"} response = self.client.put(path=reverse('api_infrastructure_fields', kwargs={'object_ptr': self.field.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data['name'], updates['name']) self.assertEquals(data['label'], updates['label']) self.assertEquals(data['field_type'], updates['field_type']) self.assertEquals(data['required'], updates['required']) self.assertEquals(data['details'], updates['details']) def test_get_types_list(self): """ Test index page api/infrastructure/types """ response = self.client.get(path=reverse('api_infrastructure_types'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_type(self): response = self.client.get(path=reverse('api_infrastructure_types', kwargs={'object_ptr': self.type.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_type(self): updates = {"name": "Api type", "parent": None, "details": "api test details", "fields": [self.field.id]} response = self.client.put(path=reverse('api_infrastructure_types', kwargs={'object_ptr': self.type.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data['name'], updates['name']) self.assertIsNone(data['parent']) self.assertEquals(data['details'], updates['details']) for i, field in enumerate(data['fields']): self.assertEquals(field['id'], updates['fields'][i]) def test_get_statuses_list(self): """ Test index page api/infrastructure/types """ response = self.client.get(path=reverse('api_infrastructure_statuses'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_status(self): response = self.client.get(path=reverse('api_infrastructure_statuses', kwargs={'object_ptr': self.status.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_status(self): updates = {"name": "Api type", "active": True, "hidden": False, "details": "Api details"} response = self.client.put(path=reverse('api_infrastructure_statuses', kwargs={'object_ptr': self.status.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data['name'], updates['name']) self.assertEquals(data['active'], updates['active']) self.assertEquals(data['hidden'], updates['hidden']) self.assertEquals(data['details'], updates['details']) def test_get_services(self): """ Test index page api/infrastructure/service_records """ response = self.client.get(path=reverse('api_infrastructure_service_records'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_service(self): response = self.client.get(path=reverse('api_infrastructure_service_records', kwargs={'object_ptr': self.servicing.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_service(self): updates = {"name": "Api servicing", "items": [self.item.id], "start_date": "2011-06-01", "expiry_date": "2011-10-01", "details": "Api details"} response = self.client.put(path=reverse('api_infrastructure_service_records', kwargs={'object_ptr': self.servicing.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data['name'], updates['name']) for i, item in enumerate(data['items']): self.assertEquals(item['id'], updates['items'][i]) self.assertEquals(data['start_date'], updates['start_date']) self.assertEquals(data['expiry_date'], updates['expiry_date']) self.assertEquals(data['details'], updates['details']) def test_get_items_list(self): """ Test index page api/infrastructure/items """ response = self.client.get(path=reverse('api_infrastructure_items'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_item(self): response = self.client.get(path=reverse('api_infrastructure_items', kwargs={'object_ptr': self.item.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_item(self): pass updates = {"name": "Close_API", "item_type": self.type.id, "status": self.status.id, "test___1": "api test"} response = self.client.put(path=reverse('api_infrastructure_items', kwargs={'object_ptr': self.item.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers) print response.content self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data['name'], updates['name']) self.assertEquals(data['item_type']['id'], updates['item_type']) self.assertEquals(data['status']['id'], updates['status']) self.assertEquals(data['itemvalue_set'][0]["value"], updates['test___1'])
class InfrastructureViewsTest(TestCase): "Infrastructure functional tests for views" username = "******" password = "******" prepared = False def setUp(self): "Initial Setup" if not self.prepared: self.group, created = Group.objects.get_or_create(name='test') duser, created = DjangoUser.objects.get_or_create(username=self.username) duser.set_password(self.password) duser.save() self.user, created = User.objects.get_or_create(user=duser) self.user.save() perspective, created = Perspective.objects.get_or_create(name='default') perspective.set_default_user() perspective.save() ModuleSetting.set('default_perspective', perspective.id) self.type = ItemType(name='test') self.type.set_default_user() self.type.save() self.status = ItemStatus(name='test') self.status.set_default_user() self.status.save() self.field = ItemField(name='test', label='test', field_type='text') self.field.set_default_user() self.field.save() self.item = Item(name='test', item_type=self.type, status=self.status) self.item.set_default_user() self.item.save() self.value = ItemValue(field=self.field, item=self.item) self.value.save() self.servicing = ItemServicing(name='test') self.servicing.set_default_user() self.servicing.save() self.client = Client() self.prepared = True ###################################### # Testing views when user is logged in ###################################### def test_index_login(self): "Test index page with login at /infrastructure/" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure')) self.assertEquals(response.status_code, 200) def test_index_infrastructure_login(self): "Test index page with login at /infrastructure/index/" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_index')) self.assertEquals(response.status_code, 200) def test_infrastructure_index_owned(self): "Test index page with login at /infrastructure/owned/" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_index_owned')) self.assertEquals(response.status_code, 200) # Type def test_infrastructure_type_add(self): "Test index page with login at /infrastructure/type/add/" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_type_add')) self.assertEquals(response.status_code, 200) def test_infrastructure_type_view(self): "Test index page with login at /infrastructure/type/view/<type_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_type_view', args=[self.type.id])) self.assertEquals(response.status_code, 200) def test_infrastructure_type_edit(self): "Test index page with login at /infrastructure/type/edit/<type_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_type_edit', args=[self.type.id])) self.assertEquals(response.status_code, 200) def test_infrastructure_type_delete(self): "Test index page with login at /infrastructure/type/delete/<type_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_type_delete', args=[self.type.id])) self.assertEquals(response.status_code, 200) # Field def test_infrastructure_field_add(self): "Test index page with login at /infrastructure/field/add/" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_field_add')) self.assertEquals(response.status_code, 200) def test_infrastructure_field_view(self): "Test index page with login at /infrastructure/field/view/<field_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_field_view', args=[self.field.id])) self.assertEquals(response.status_code, 200) def test_infrastructure_field_edit(self): "Test index page with login at /infrastructure/field/edit/<field_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_field_edit', args=[self.field.id])) self.assertEquals(response.status_code, 200) def test_infrastructure_field_del(self): "Test index page with login at /infrastructure/field/delete/<field_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_field_delete', args=[self.field.id])) self.assertEquals(response.status_code, 200) # Status def test_infrastructure_status_add(self): "Test index page with login at /infrastructure/status/add/" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_status_add')) self.assertEquals(response.status_code, 200) def test_infrastructure_status_view(self): "Test index page with login at /infrastructure/status/view/<status_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_status_view', args=[self.status.id])) self.assertEquals(response.status_code, 200) def test_infrastructure_status_edit(self): "Test index page with login at /infrastructure/status/edit/<status_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_status_edit', args=[self.status.id])) self.assertEquals(response.status_code, 200) def test_infrastructure_status_del(self): "Test index page with login at /infrastructure/status/delete/<status_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_status_delete', args=[self.status.id])) self.assertEquals(response.status_code, 200) # Item def test_infrastructure_item_add(self): "Test index page with login at /infrastructure/item/add/" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_item_add')) self.assertEquals(response.status_code, 200) def test_infr_item_add_typed(self): "Test index page with login at /infrastructure/item/add/<type_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_item_add_typed', args=[self.type.id])) self.assertEquals(response.status_code, 200) def test_infrastructure_item_view(self): "Test index page with login at /infrastructure/item/view/<item_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_item_view', args=[self.item.id])) self.assertEquals(response.status_code, 200) def test_infrastructure_item_edit(self): "Test index page with login at /infrastructure/item/edit/<item_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_item_edit', args=[self.item.id])) self.assertEquals(response.status_code, 200) def test_infrastructure_item_del(self): "Test index page with login at /infrastructure/item/delete/<item_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_item_delete', args=[self.item.id])) self.assertEquals(response.status_code, 200) # Service Record def test_infr_service_record_index(self): "Test index page with login at /infrastructure/service_record/index/" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_service_record_index')) self.assertEquals(response.status_code, 200) def test_infr_service_record_add(self): "Test index page with login at /infrastructure/service_record/add/" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_service_record_add')) self.assertEquals(response.status_code, 200) def test_infr_service_record_view(self): "Test index page with login at /infrastructure/service_record/view/<service_record_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_service_record_view', args=[self.servicing.id])) self.assertEquals(response.status_code, 200) def test_infr_service_record_edit(self): "Test index page with login at /infrastructure/service_record/edit/<service_record_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_service_record_edit', args=[self.servicing.id])) self.assertEquals(response.status_code, 200) def test_infr_service_record_delete(self): "Test index page with login at /infrastructure/service_record/delete/<service_record_id>" response = self.client.post('/accounts/login', {'username': self.username, 'password': self.password }) self.assertRedirects(response, '/') response = self.client.get(reverse('infrastructure_service_record_delete', args=[self.servicing.id])) self.assertEquals(response.status_code, 200) ###################################### # Testing views when user is not logged in ###################################### def test_index(self): "Testing /infrastructure/" response = self.client.get('/infrastructure/') # Redirects as unauthenticated self.assertRedirects(response, reverse('user_login')) def test_index_infrastructure_out(self): "Testing /infrastructure/index/" response = self.client.get(reverse('infrastructure_index')) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_index_owned_out(self): "Testing /infrastructure/owned/" response = self.client.get(reverse('infrastructure_index_owned')) self.assertRedirects(response, reverse('user_login')) # Type def test_infrastructure_type_add_out(self): "Testing /infrastructure/type/add/" response = self.client.get(reverse('infrastructure_type_add')) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_type_view_out(self): "Testing /infrastructure/type/view/<type_id>" response = self.client.get(reverse('infrastructure_type_view', args=[self.type.id])) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_type_edit_out(self): "Testing /infrastructure/type/edit/<type_id>" response = self.client.get(reverse('infrastructure_type_edit', args=[self.type.id])) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_type_delete_out(self): "Testing /infrastructure/type/delete/<type_id>" response = self.client.get(reverse('infrastructure_type_delete', args=[self.type.id])) self.assertRedirects(response, reverse('user_login')) # Field def test_infrastructure_field_add_out(self): "Testing /infrastructure/field/add/" response = self.client.get(reverse('infrastructure_field_add')) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_field_view_out(self): "Testing /infrastructure/field/view/<field_id>" response = self.client.get(reverse('infrastructure_field_view', args=[self.field.id])) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_field_edit_out(self): "Testing /infrastructure/field/edit/<field_id>" response = self.client.get(reverse('infrastructure_field_edit', args=[self.field.id])) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_field_del_out(self): "Testing /infrastructure/field/delete/<field_id>" response = self.client.get(reverse('infrastructure_field_delete', args=[self.field.id])) self.assertRedirects(response, reverse('user_login')) # Status def test_infrastructure_status_add_out(self): "Testing /infrastructure/status/add/" response = self.client.get(reverse('infrastructure_status_add')) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_status_view_out(self): "Testing /infrastructure/status/view/<status_id>" response = self.client.get(reverse('infrastructure_status_view', args=[self.status.id])) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_status_edit_out(self): "Testing /infrastructure/status/edit/<status_id>" response = self.client.get(reverse('infrastructure_status_edit', args=[self.status.id])) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_status_del_out(self): "Testing /infrastructure/status/delete/<status_id>" response = self.client.get(reverse('infrastructure_status_delete', args=[self.status.id])) self.assertRedirects(response, reverse('user_login')) # Item def test_infrastructure_item_add_out(self): "Testing /infrastructure/item/add/" response = self.client.get(reverse('infrastructure_item_add')) self.assertRedirects(response, reverse('user_login')) def test_infr_item_add_typed_out(self): "Testing /infrastructure/item/add/<type_id>" response = self.client.get(reverse('infrastructure_item_add_typed', args=[self.type.id])) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_item_view_out(self): "Testing /infrastructure/item/view/<item_id>" response = self.client.get(reverse('infrastructure_item_view', args=[self.item.id])) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_item_edit_out(self): "Testing /infrastructure/item/edit/<item_id>" response = self.client.get(reverse('infrastructure_item_edit', args=[self.item.id])) self.assertRedirects(response, reverse('user_login')) def test_infrastructure_item_del_out(self): "Testing /infrastructure/item/delete/<item_id>" response = self.client.get(reverse('infrastructure_item_delete', args=[self.item.id])) self.assertRedirects(response, reverse('user_login')) # Service Record def test_infr_service_record_index_out(self): "Testing /infrastructure/service_record/index/" response = self.client.get(reverse('infrastructure_service_record_index')) self.assertRedirects(response, reverse('user_login')) def test_infr_service_record_add_out(self): "Testing /infrastructure/service_record/add/" response = self.client.get(reverse('infrastructure_service_record_add')) self.assertRedirects(response, reverse('user_login')) def test_infr_service_record_view_out(self): "Testing /infrastructure/service_record/view/<service_record_id>" response = self.client.get(reverse('infrastructure_service_record_view', args=[self.servicing.id])) self.assertRedirects(response, reverse('user_login')) def test_infr_service_record_edit_out(self): "Testing /infrastructure/service_record/edit/<service_record_id>" response = self.client.get(reverse('infrastructure_service_record_edit', args=[self.servicing.id])) self.assertRedirects(response, reverse('user_login')) def test_infr_service_record_delete_out(self): "Testing /infrastructure/service_record/delete/<service_record_id>" response = self.client.get(reverse('infrastructure_service_record_delete', args=[self.servicing.id])) self.assertRedirects(response, reverse('user_login'))