def test_model_group(self): "Test Group model" obj = Group(name='test') obj.save() self.assertEquals('test', obj.name) self.assertNotEquals(obj.id, None) obj.delete()
def test_model_user(self): "Test User model" username = "******" password = "******" user = DjangoUser(username=username, password=password) user.set_password(password) user.save() self.assertEquals('test', user.username) self.assertNotEquals(user.id, None) group = Group(name='test') group.save() self.assertEquals('test', group.name) self.assertNotEquals(group.id, None) profile = User(user=user, default_group=group) profile.save() self.assertEquals(user, profile.user) self.assertNotEquals(profile.id, None) profile.delete() group.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 DocumentsViewsTest(TestCase): "Documents 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: 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.folder = Folder(name='test') self.folder.set_default_user() self.folder.save() self.document = Document(title='test_document', folder=self.folder) self.document.set_default_user() self.document.save() self.file = File(name='test_file', folder=self.folder) self.file.set_default_user() self.file.save() self.link = WebLink(title='test', folder=self.folder, url='test') self.link.set_default_user() self.link.save() self.client = Client() self.prepared = True def test_unauthenticated_access(self): "Test index page at /api/documents/folders" response = self.client.get('/api/documents/folders') # Redirects as unauthenticated self.assertEquals(response.status_code, 401) def test_get_folders_list(self): """ Test index page api/documents/folders """ response = self.client.get(path=reverse('api_documents_folders'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_folder(self): response = self.client.get(path=reverse('api_documents_folders', kwargs={'object_ptr': self.folder.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_folder(self): updates = { "name": "Api_folder_name" } response = self.client.put(path=reverse('api_documents_folders', kwargs={'object_ptr': self.folder.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']) def test_get_files_list(self): """ Test index page api/documents/files """ response = self.client.get(path=reverse('api_documents_files'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_file(self): response = self.client.get(path=reverse('api_documents_files', kwargs={'object_ptr': self.file.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) # def test_update_file(self): # updates = { "name": "Api_folder_name" } # response = self.client.put(path=reverse('api_documents_files', kwargs={'object_ptr': self.file.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']) def test_get_documents_list(self): """ Test index page api/documents/documents """ response = self.client.get(path=reverse('api_documents_documents'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_document(self): response = self.client.get(path=reverse('api_documents_documents', kwargs={'object_ptr': self.document.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_document(self): updates = { "title": "Api_title", "folder": self.folder.id, "body": "Api test body" } response = self.client.put(path=reverse('api_documents_documents', kwargs={'object_ptr': self.document.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['title'], updates['title']) self.assertEquals(data['folder']['id'], updates['folder']) self.assertEquals(data['body'], updates['body']) def test_get_weblinks_list(self): """ Test index page api/documents/weblinks """ response = self.client.get(path=reverse('api_documents_weblinks'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_weblink(self): response = self.client.get(path=reverse('api_documents_weblinks', kwargs={'object_ptr': self.link.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_weblink(self): updates = { "title": "Api_title", "folder": self.folder.id, "url": "http://Api-test-body" } response = self.client.put(path=reverse('api_documents_weblinks', kwargs={'object_ptr': self.link.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['title'], updates['title']) self.assertEquals(data['folder']['id'], updates['folder']) self.assertEquals(data['url'], updates['url'])
class MessagingApiTest(TestCase): "Messaging 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.contact_type = ContactType(name='test') self.contact_type.set_default_user() self.contact_type.save() self.contact = Contact(name='test', contact_type=self.contact_type) self.contact.set_default_user() self.contact.save() self.user_contact = Contact(name='test', related_user=self.user.get_profile(), contact_type=self.contact_type) self.user_contact.set_user(self.user) self.user_contact.save() self.stream = MessageStream(name='test') self.stream.set_default_user() self.stream.save() self.mlist = MailingList(name='test', from_contact=self.contact) self.mlist.set_default_user() self.mlist.save() self.message = Message(title='test', body='test', author=self.contact, stream=self.stream) self.message.set_default_user() self.message.save() self.client = Client() self.prepared = True def test_unauthenticated_access(self): "Test index page at /api/messaging/mlist" response = self.client.get('/api/messaging/mlist') # Redirects as unauthenticated self.assertEquals(response.status_code, 401) def test_get_mlist(self): """ Test index page api/messaging/mlist """ response = self.client.get(path=reverse('api_messaging_mlist'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_one_mlist(self): response = self.client.get(path=reverse('api_messaging_mlist', kwargs={'object_ptr': self.mlist.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_mlist(self): updates = {"name": "API mailing list", "description": "API description update", "from_contact": self.contact.id, "members": [self.contact.id,]} response = self.client.put(path=reverse('api_messaging_mlist', kwargs={'object_ptr': self.mlist.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['description'], updates['description']) self.assertEquals(data['from_contact']['id'], updates['from_contact']) for i, member in enumerate(data['members']): self.assertEquals(member['id'], updates['members'][i]) def test_get_streams(self): """ Test index page api/messaging/streams """ response = self.client.get(path=reverse('api_messaging_streams'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_stream(self): response = self.client.get(path=reverse('api_messaging_streams', kwargs={'object_ptr': self.stream.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_stream(self): updates = {"name": "API stream", } response = self.client.put(path=reverse('api_messaging_streams', kwargs={'object_ptr': self.stream.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']) def test_get_messages(self): """ Test index page api/messaging/messages """ response = self.client.get(path=reverse('api_messaging_messages'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_message(self): response = self.client.get(path=reverse('api_messaging_messages', kwargs={'object_ptr': self.message.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_send_message(self): updates = {"title": "API message title", "body": "Test body", "stream": self.stream.id, "multicomplete_recipients": u'*****@*****.**'} response = self.client.post(path=reverse('api_messaging_messages'), 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['title'], updates['title']) self.assertEquals(data['body'], updates['body']) self.assertEquals(data['stream']['id'], updates['stream']) def test_reply_to_message(self): updates = {"title": "API test", "body": "Test body", "stream": self.stream.id, "multicomplete_recipients": u'*****@*****.**'} response = self.client.put(path=reverse('api_messaging_messages', kwargs={'object_ptr': self.message.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.assertNotEquals(data['title'], updates['title']) self.assertEquals(data['body'], updates['body']) self.assertEquals(data['stream']['id'], updates['stream'])
class KnowledgeViewsTest(TestCase): "Knowledge functional tests for views" username = "******" password = "******" prepared = False authentication_headers ={"CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION" : "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk" } content_type ='application/json' prepared = False 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.folder = KnowledgeFolder(name='test', treepath='test') self.folder.set_default_user() self.folder.save() self.category = KnowledgeCategory(name='test', treepath='test') self.category.set_default_user() self.category.save() self.item = KnowledgeItem(name='test', folder=self.folder, category=self.category, treepath='test') self.item.set_default_user() self.item.save() # parent folder self.parent = KnowledgeFolder(name='test', treepath='test') self.parent.set_default_user() self.parent.save() self.client = Client() self.prepared = True def test_unauthenticated_access(self): "Test index page at /api/knowledge/folders" response = self.client.get('/api/knowledge/folders') # Redirects as unauthenticated self.assertEquals(response.status_code, 401) def test_get_folders_list(self): """ Test index page api/knowledge/folders """ response = self.client.get(path=reverse('api_knowledge_folders'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_folder(self): response = self.client.get(path=reverse('api_knowledge_folders', kwargs={'object_ptr': self.folder.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_folder(self): updates = {'name': 'Api folder update', 'parent': self.parent.id, 'details': '<p>api details</p>'} response = self.client.put(path=reverse('api_knowledge_folders', kwargs={'object_ptr': self.folder.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(updates["name"], data["name"]) self.assertEquals(updates["parent"], data["parent"]["id"]) self.assertEquals(updates["details"], data["details"]) def test_get_categories_list(self): """ Test index page api/knowledge/categories """ response = self.client.get(path=reverse('api_knowledge_categories'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_category(self): response = self.client.get(path=reverse('api_knowledge_categories', kwargs={'object_ptr': self.category.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_category(self): updates = {'name': 'Api catagory update', 'details': '<p>api details</p>'} response = self.client.put(path=reverse('api_knowledge_categories', kwargs={'object_ptr': self.category.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(updates["name"], data["name"]) self.assertEquals(updates["details"], data["details"]) def test_get_items_list(self): """ Test index page api/knowledge/items """ response = self.client.get(path=reverse('api_knowledge_items'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_item(self): response = self.client.get(path=reverse('api_knowledge_items', kwargs={'object_ptr': self.item.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_item(self): updates = {'name': 'Api item update', 'folder': self.folder.id, 'category': self.category.id, 'body': '<p>api body</p>'} response = self.client.put(path=reverse('api_knowledge_items', kwargs={'object_ptr': self.item.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers) self.assertEquals(response.status_code, 400)
class FinanceAPITest(TestCase): "Finance api tests" 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() User.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.contact_type = ContactType(name="test") self.contact_type.set_default_user() self.contact_type.save() self.contact = Contact(name="test", contact_type=self.contact_type) self.contact.set_default_user() self.contact.save() self.category = Category(name="test") self.category.set_default_user() self.category.save() self.equity = Equity(issue_price=10, sell_price=10, issuer=self.contact, owner=self.contact) self.equity.set_default_user() self.equity.save() self.asset = Asset(name="test", owner=self.contact) self.asset.set_default_user() self.asset.save() self.tax = Tax(name="test", rate=10) self.tax.set_default_user() self.tax.save() self.currency = Currency(code="GBP", name="Pounds", symbol="L", is_default=True) self.currency.set_default_user() self.currency.save() self.account = Account(name="test", owner=self.contact, balance_currency=self.currency) self.account.set_default_user() self.account.save() self.liability = Liability( name="test", source=self.contact, target=self.contact, account=self.account, value=10, value_currency=self.currency, ) self.liability.set_default_user() self.liability.save() self.transaction = Transaction( name="test", account=self.account, source=self.contact, target=self.contact, value=10, value_currency=self.currency, ) self.transaction.set_default_user() self.transaction.save() self.client = Client() self.prepared = True def test_unauthenticated_access(self): "Test index page at /api/finance/currencies" response = self.client.get("/api/finance/currencies") # Redirects as unauthenticated self.assertEquals(response.status_code, 401) def test_get_currencies_list(self): """ Test index page api/finance/currencies """ response = self.client.get(path=reverse("api_finance_currencies"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_currency(self): response = self.client.get( path=reverse("api_finance_currencies", kwargs={"object_ptr": self.currency.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_currency(self): updates = {"code": "RUB", "name": "api RUB", "factor": "10.00", "is_active": True} response = self.client.put( path=reverse("api_finance_currencies", kwargs={"object_ptr": self.currency.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["code"], updates["code"]) self.assertEquals(data["name"], updates["name"]) self.assertEquals(data["factor"], updates["factor"]) self.assertEquals(data["is_active"], updates["is_active"]) def test_get_taxes_list(self): """ Test index page api/finance/taxes """ response = self.client.get(path=reverse("api_finance_taxes"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_tax(self): response = self.client.get( path=reverse("api_finance_taxes", kwargs={"object_ptr": self.tax.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_tax(self): updates = {"name": "API TEST TAX", "rate": "20.00", "compound": False} response = self.client.put( path=reverse("api_finance_taxes", kwargs={"object_ptr": self.tax.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["rate"], updates["rate"]) self.assertEquals(data["compound"], updates["compound"]) def test_get_categories_list(self): """ Test index page api/finance/categories """ response = self.client.get(path=reverse("api_finance_categories"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_category(self): response = self.client.get( path=reverse("api_finance_categories", kwargs={"object_ptr": self.category.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_category(self): updates = {"name": "Api category", "details": "api details"} response = self.client.put( path=reverse("api_finance_categories", kwargs={"object_ptr": self.category.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["details"], updates["details"]) def test_get_assets_list(self): """ Test index page api/finance/assets """ response = self.client.get(path=reverse("api_finance_assets"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_asset(self): response = self.client.get( path=reverse("api_finance_assets", kwargs={"object_ptr": self.asset.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_asset(self): updates = { "current_value": "20.0", "owner": self.contact.id, "asset_type": "fixed", "name": "Api name", "initial_value": "40.0", } response = self.client.put( path=reverse("api_finance_assets", kwargs={"object_ptr": self.asset.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["owner"]["id"], updates["owner"]) self.assertEquals(data["asset_type"], updates["asset_type"]) self.assertEquals(data["initial_value"], updates["initial_value"]) self.assertEquals(data["current_value"], updates["current_value"]) def test_get_accounts_list(self): """ Test index page api/finance/accounts """ response = self.client.get(path=reverse("api_finance_accounts"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_account(self): response = self.client.get( path=reverse("api_finance_accounts", kwargs={"object_ptr": self.account.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_account(self): updates = { "owner": self.user.id, "balance_display": 40.0, "name": "api test name", "balance_currency": self.currency.id, } response = self.client.put( path=reverse("api_finance_accounts", kwargs={"object_ptr": self.account.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["owner"]["id"], updates["owner"]) self.assertEquals(data["balance_display"], updates["balance_display"]) self.assertEquals(data["balance_currency"]["id"], updates["balance_currency"]) def test_get_equities_list(self): """ Test index page api/finance/equities""" response = self.client.get(path=reverse("api_finance_equities"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_equity(self): response = self.client.get( path=reverse("api_finance_equities", kwargs={"object_ptr": self.equity.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_account(self): updates = { "issue_price": "100.0", "equity_type": "warrant", "sell_price": "50.0", "amount": 100, "purchase_date": "2011-06-06", "owner": self.contact.id, "issuer": self.contact.id, } response = self.client.put( path=reverse("api_finance_equities", kwargs={"object_ptr": self.equity.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["issue_price"], updates["issue_price"]) self.assertEquals(data["equity_type"], updates["equity_type"]) self.assertEquals(data["sell_price"], updates["sell_price"]) self.assertEquals(data["amount"], updates["amount"]) self.assertEquals(data["purchase_date"], updates["purchase_date"]) self.assertEquals(data["owner"]["id"], updates["owner"]) self.assertEquals(data["issuer"]["id"], updates["issuer"]) self.assertEquals(data["issuer"]["id"], updates["issuer"]) def test_get_liabilities_list(self): """ Test index page api/finance/liabilities""" response = self.client.get(path=reverse("api_finance_liabilities"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_liability(self): response = self.client.get( path=reverse("api_finance_liabilities", kwargs={"object_ptr": self.liability.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_liability(self): updates = { "account": self.account.id, "target": self.contact.id, "value_display": "20.0", "name": "api test name", "value_currency": self.currency.id, } response = self.client.put( path=reverse("api_finance_liabilities", kwargs={"object_ptr": self.liability.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["target"]["id"], updates["target"]) self.assertEquals(data["account"]["id"], updates["account"]) self.assertEquals(data["value_display"], updates["value_display"]) self.assertEquals(data["value_currency"]["id"], updates["value_currency"]) def test_get_transactions_list(self): """ Test index page api/finance/transactions""" response = self.client.get(path=reverse("api_finance_transactions"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_transaction(self): response = self.client.get( path=reverse("api_finance_transactions", kwargs={"object_ptr": self.transaction.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_transaction(self): updates = { "value_display": "1000.0", "account": self.account.id, "name": "api test name", "value_currency": self.currency.id, "datetime": "2011-03-21 11:04:42", "target": self.contact.id, "account": self.account.id, "source": self.contact.id, } response = self.client.put( path=reverse("api_finance_transactions", kwargs={"object_ptr": self.transaction.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["value_display"], updates["value_display"]) self.assertEquals(data["account"]["id"], updates["account"]) self.assertEquals(data["value_currency"]["id"], updates["value_currency"]) self.assertEquals(data["datetime"], updates["datetime"]) self.assertEquals(data["target"]["id"], updates["target"]) self.assertEquals(data["account"]["id"], updates["account"]) self.assertEquals(data["source"]["id"], updates["source"])
class MiddlewareChatTest(TestCase): "Midleware chat tests" username = "******" password = "******" prepared = False def setUp(self): "Initial Setup" if not self.prepared: 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.client = Client() self.prepared = True def test_chat_get_new_messages(self): "Test get_new_messages" response = self.client.post('/chat', {'json': '{"cmd":"Get", "location":"#"}'}) self.assertEqual(response.status_code, 200) def test_chat_connect(self): "Test connect" response = self.client.post('/chat', {'json': '{"cmd":"Connect", "location":"#"}'}) self.assertEqual(response.status_code, 200) def test_chat_disconnect(self): "Test disconnect" response = self.client.post('/chat', {'json': '{"cmd":"Disconnect", "location":"#"}'}) self.assertEqual(response.status_code, 200) def test_chat_add_new_message(self): "Test add_new_message" response = self.client.post('/chat', {'json': '{"cmd":"Message","data":{"id":"test_b5e6d0470a5f4656c3bc77f879c3dbbc","text":"test message"},"location":"#"}'}) self.assertEqual(response.status_code, 200) def test_chat_exit_from_conference(self): "Test exit_from_conference" response = self.client.post('/chat', {'json': '{"cmd":"Exit","data":{"id":"test_b5e6d0470a5f4656c3bc77f879c3dbbc"},"location":"#"}'}) self.assertEqual(response.status_code, 200) def test_chat_add_users_in_conference(self): "Test add_users_in_conference" response = self.client.post('/chat', {'json': '{"cmd":"Add","data":{"id":"guest_006f721c4a59a44d969b9f73fb6360a5","users":["test"]},"location":"#"}'}) self.assertEqual(response.status_code, 200) def test_chat_create_conference(self): "Test create_conference" response = self.client.post('/chat', {'json': '{"cmd":"Create","data":{"title":["Admin"],"users":["admin"]},"location":"#"}'}) self.assertEqual(response.status_code, 200)
class EventsViewsTest(TestCase): "Events 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.event = Event(name='TestStatus', end=datetime.now()) self.event.set_default_user() self.event.save() self.client = Client() self.prepared = True def test_unauthenticated_access(self): "Test index page at /api/calendar/events" response = self.client.get('/api/calendar/events') # Redirects as unauthenticated self.assertEquals(response.status_code, 401) def test_get_events_list(self): """ Test index page api/infrastructure/types """ response = self.client.get(path=reverse('api_events'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_field(self): response = self.client.get(path=reverse('api_events', kwargs={'object_ptr': self.event.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_field(self): updates = {"name": "Api_name", "details": "Api details", "start": "2011-03-01 01:12:09", "end": "2011-03-09 13:05:09"} response = self.client.put(path=reverse('api_events', kwargs={'object_ptr': self.event.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['details'], updates['details']) self.assertEquals(data['start'], updates['start']) self.assertEquals(data['end'], updates['end'])
class ProjectsAPITest(TestCase): "Projects 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() User.objects.all().delete() # Create objects try: self.group = Group.objects.get(name="api_test") except Group.DoesNotExist: Group.objects.all().delete() self.group = Group(name="api_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.contact_type = ContactType(name="api_test") self.contact_type.set_default_user() self.contact_type.save() self.contact = Contact(name="api_test", contact_type=self.contact_type) self.contact.set_default_user() self.contact.save() self.project = Project(name="api_test", manager=self.contact, client=self.contact) self.project.set_default_user() self.project.save() self.status = TaskStatus(name="api_test") self.status.set_default_user() self.status.save() self.milestone = Milestone(name="api_test", project=self.project, status=self.status) self.milestone.set_default_user() self.milestone.save() self.task = Task(name="api_test", project=self.project, status=self.status, priority=3) self.task.set_default_user() self.task.save() self.time_slot = TaskTimeSlot( task=self.task, details="api_test", time_from=datetime.now(), user=self.user.get_profile() ) self.time_slot.set_default_user() self.time_slot.save() self.parent = Project(name="api_test") self.parent.set_default_user() self.parent.save() self.parent_task = Task(name="api_test", project=self.project, status=self.status, priority=3) self.parent_task.set_default_user() self.parent_task.save() self.client = Client() self.prepared = True def test_unauthenticated_access(self): "Test index page at /projects" response = self.client.get("/api/projects/projects") # Redirects as unauthenticated self.assertEquals(response.status_code, 401) # Get info about projects, milestones, status, tasks, tasktimes. def test_get_project_list(self): """ Test index page api/projects """ response = self.client.get(path=reverse("api_projects"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_status_list(self): """ Test index page api/status """ response = self.client.get(path=reverse("api_projects_status"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_milestones_list(self): """ Test index page api/milestones """ response = self.client.get(path=reverse("api_projects_milestones"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_task_list(self): """ Test index page api/tasks """ response = self.client.get(path=reverse("api_projects_tasks"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_tasktimes_list(self): """ Test index page api/tasktimes """ response = self.client.get(path=reverse("api_projects_tasktimes"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_project(self): response = self.client.get( reverse("api_projects", kwargs={"object_ptr": self.project.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data["id"], self.project.id) self.assertEquals(data["name"], self.project.name) self.assertEquals(data["details"], self.project.details) def test_get_status(self): response = self.client.get( reverse("api_projects_status", kwargs={"object_ptr": self.status.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data["id"], self.status.id) self.assertEquals(data["name"], self.status.name) def test_get_milestone(self): response = self.client.get( reverse("api_projects_milestones", kwargs={"object_ptr": self.milestone.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data["id"], self.milestone.id) self.assertEquals(data["name"], self.milestone.name) self.assertEquals(data["project"]["id"], self.milestone.project.id) self.assertEquals(data["status"]["id"], self.milestone.status.id) def test_get_task(self): response = self.client.get( reverse("api_projects_tasks", kwargs={"object_ptr": self.task.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data["id"], self.task.id) self.assertEquals(data["name"], self.task.name) self.assertEquals(data["priority"], self.task.priority) self.assertEquals(data["project"]["id"], self.task.project.id) self.assertEquals(data["status"]["id"], self.task.status.id) def test_get_timeslot(self): response = self.client.get( reverse("api_projects_tasktimes", kwargs={"object_ptr": self.time_slot.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data["id"], self.time_slot.id) self.assertEquals(data["task"]["id"], self.time_slot.task.id) # Common test def test_common_project(self): # create new project new_project = {"name": "api test", "details": "<p>test details</p>"} response = self.client.post( reverse("api_projects"), data=json.dumps(new_project), content_type=self.content_type, **self.authentication_headers ) # print response.request self.assertEquals(response.status_code, 200) # check data in response data = json.loads(response.content) self.assertEquals(data["name"], new_project["name"]) self.assertEquals(data["details"], new_project["details"]) project_id = data["id"] # get info about new project response = self.client.get( path=reverse("api_projects", kwargs={"object_ptr": project_id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) # get statuses list response = self.client.get(path=reverse("api_projects_status"), **self.authentication_headers) self.assertEquals(response.status_code, 200) statuses = json.loads(response.content) fstatus = statuses[0]["id"] # create new task status new_status = {"name": "Open api test", "active": True, "hidden": False, "details": "<p>test details</p>"} response = self.client.post( reverse("api_projects_status"), data=json.dumps(new_status), content_type=self.content_type, **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data["name"], new_status["name"]) self.assertEquals(data["active"], new_status["active"]) self.assertEquals(data["hidden"], new_status["hidden"]) self.assertEquals(data["details"], new_status["details"]) sstatus = data["id"] # create new milestone new_milestone = { "name": "api test milestone", "status": fstatus, "project": project_id, "start_date": "2011-06-09 12:00:00", "details": "<p>test details</p>", } response = self.client.post( reverse("api_projects_milestones"), data=json.dumps(new_milestone), content_type=self.content_type, **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data["name"], new_milestone["name"]) self.assertEquals(data["status"]["id"], new_milestone["status"]) self.assertEquals(data["project"]["id"], new_milestone["project"]) self.assertEquals(data["details"], new_milestone["details"]) milestone_id = data["id"] # create new task new_task = { "name": "api test task", "status": sstatus, "project": project_id, "milestone": milestone_id, "priority": 5, "start_date": "2011-06-02 12:00:00", "estimated_time": 5000, "details": "<p>test details</p>", } response = self.client.post( reverse("api_projects_tasks"), data=json.dumps(new_task), content_type=self.content_type, **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data["name"], new_task["name"]) self.assertEquals(data["priority"], new_task["priority"]) self.assertEquals(data["status"]["id"], new_task["status"]) self.assertEquals(data["project"]["id"], new_task["project"]) self.assertEquals(data["milestone"]["id"], new_task["milestone"]) self.assertEquals(data["estimated_time"], new_task["estimated_time"]) self.assertEquals(data["details"], new_task["details"]) task_id = data["id"] # create new subtask new_sub_task = { "name": "api test task", "status": sstatus, "parent": task_id, "project": project_id, "priority": 5, "start_date": "2011-06-02 13:00:00", "estimated_time": 2500, "details": "<p>test details</p>", } response = self.client.post( reverse("api_projects_tasks"), data=json.dumps(new_sub_task), content_type=self.content_type, **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data["name"], new_sub_task["name"]) self.assertEquals(data["priority"], new_sub_task["priority"]) self.assertEquals(data["status"]["id"], new_sub_task["status"]) self.assertEquals(data["parent"]["id"], new_sub_task["parent"]) self.assertEquals(data["project"]["id"], new_sub_task["project"]) self.assertEquals(data["estimated_time"], new_sub_task["estimated_time"]) self.assertEquals(data["details"], new_sub_task["details"]) sub_task_id = data["id"] # create task time new_tasktime = {"task": task_id, "minutes": 400, "details": "<p>test details</p>"} response = self.client.post( reverse("api_projects_tasktimes"), data=json.dumps(new_tasktime), content_type=self.content_type, **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(data["task"]["id"], new_tasktime["task"]) self.assertEquals(data["details"], new_tasktime["details"]) tasktime_id = data["id"] # start task time response = self.client.get( path=reverse("api_projects_tasktime_start", kwargs={"task_id": sub_task_id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) slot_id = data["id"] sleep(60) # stop task time response = self.client.post( reverse("api_projects_tasktime_stop", kwargs={"slot_id": slot_id}), data=json.dumps({"details": "<p>test details</p>"}), content_type=self.content_type, **self.authentication_headers ) self.assertEquals(response.status_code, 200) # delete task time response = self.client.delete( reverse("api_projects_tasktimes", kwargs={"object_ptr": tasktime_id}), **self.authentication_headers ) self.assertEquals(response.status_code, 204) # delete task response = self.client.delete( reverse("api_projects_tasks", kwargs={"object_ptr": task_id}), **self.authentication_headers ) self.assertEquals(response.status_code, 204) # check subtask response = self.client.get( path=reverse("api_projects_tasks", kwargs={"object_ptr": sub_task_id}), **self.authentication_headers ) self.assertEquals(response.status_code, 404) # delete milestone response = self.client.delete( reverse("api_projects_milestones", kwargs={"object_ptr": milestone_id}), **self.authentication_headers ) self.assertEquals(response.status_code, 204) # delete status response = self.client.delete( reverse("api_projects_status", kwargs={"object_ptr": sstatus}), **self.authentication_headers ) self.assertEquals(response.status_code, 204) # delete project response = self.client.delete( reverse("api_projects", kwargs={"object_ptr": project_id}), **self.authentication_headers ) self.assertEquals(response.status_code, 204)
class CoreAPITest(TestCase): "Core api tests" username = "******" password = "******" prepared = False authentication_headers ={"CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION" : "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk" } content_type ='application/json' prepared = False def setUp(self): "Initial Setup" if not self.prepared: Object.objects.all().delete() # Create objects 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.perspective = Perspective(name='test') self.perspective.set_default_user() self.perspective.save() self.group = Group(name='test') self.group.save() self.client = Client() self.prepared = True def test_unauthenticated_access(self): "Test index page at /admin/api/users" response = self.client.get('/admin/api/users') # Redirects as unauthenticated self.assertEquals(response.status_code, 404) def test_get_ticket_groups_list(self): """ Test index page api /admin/api/groups """ response = self.client.get(path=reverse('api_admin_groups'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_group(self): response = self.client.get(path=reverse('api_admin_groups', kwargs={'accessentity_ptr': self.group.id}), **self.authentication_headers) print response.content self.assertEquals(response.status_code, 200) def test_update_group(self): updates = {'name': 'Api group name', 'details': '<p>api details</p>', 'perspective': self.perspective.id} response = self.client.put(path=reverse('api_admin_groups', kwargs={'accessentity_ptr': self.group.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['details'], updates['details']) self.assertEquals(data['perspective']['id'], updates['perspective']) def test_get_ticket_users_list(self): """ Test index page api /admin/api/users """ response = self.client.get(path=reverse('api_admin_users'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_user(self): response = self.client.get(path=reverse('api_admin_users', kwargs={'accessentity_ptr': self.user.id}), **self.authentication_headers) print response.content self.assertEquals(response.status_code, 200) def test_update_user(self): updates = {'name': 'Api user name', 'default_group': self.group.id, 'disabled': False, 'perspective': self.perspective.id} response = self.client.put(path=reverse('api_admin_users', kwargs={'accessentity_ptr': self.user.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers) self.assertEquals(response.status_code, 405) #data = json.loads(response.content) #self.assertEquals(data['name'], updates['name']) #self.assertEquals(data['disabled'], updates['disabled']) #self.assertEquals(data['default_group']['id'], updates['default_group']) #self.assertEquals(data['perspective']['id'], updates['perspective']) def test_delete_self(self): response = self.client.delete(path=reverse('api_admin_users', kwargs={'accessentity_ptr': self.user.id}), **self.authentication_headers) self.assertEquals(response.status_code, 401) def test_get_ticket_modules_list(self): """ Test index page api /admin/api/modules """ response = self.client.get(path=reverse('api_admin_modules'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_ticket_perspectives_list(self): """ Test index page api /admin/api/perspectives """ response = self.client.get(path=reverse('api_admin_perspectives'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_perspective(self): response = self.client.get(path=reverse('api_admin_perspectives', kwargs={'object_ptr': self.perspective.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_perspective(self): updates = {'name': 'Api perspective', 'details': 'Api details'} response = self.client.put(path=reverse('api_admin_perspectives', kwargs={'object_ptr': self.perspective.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['details'], updates['details'])
class ServicesViewsTest(TestCase): "Services functional tests for api" username = "******" password = "******" prepared = False authentication_headers = { "CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk", } content_type = "application/json" prepared = False def setUp(self): "Initial Setup" if not self.prepared: # Clean up first Object.objects.all().delete() User.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_user(self.user.get_profile()) perspective.save() ModuleSetting.set("default_perspective", perspective.id) self.contact_type = ContactType(name="test") self.contact_type.set_default_user() self.contact_type.save() self.contact = Contact(name="test", contact_type=self.contact_type) self.contact.set_default_user() self.contact.save() self.status = TicketStatus(name="TestStatus") self.status.set_default_user() self.status.save() self.queue = TicketQueue(name="TestQueue", default_ticket_status=self.status) self.queue.set_default_user() self.queue.save() self.ticket = Ticket(name="TestTicket", status=self.status, queue=self.queue) self.ticket.set_default_user() self.ticket.save() self.agent = ServiceAgent( related_user=self.user.get_profile(), available_from=datetime.time(9), available_to=datetime.time(17) ) self.agent.set_default_user() self.agent.save() self.service = Service(name="test") self.service.set_default_user() self.service.save() self.sla = ServiceLevelAgreement( name="test", service=self.service, client=self.contact, provider=self.contact ) self.sla.set_default_user() self.sla.save() self.client = Client() self.prepared = True def test_unauthenticated_access(self): "Test index page at /api/services/services" response = self.client.get("/api/services/services") # Redirects as unauthenticated self.assertEquals(response.status_code, 401) def test_get_ticket_statuses_list(self): """ Test index page api/services/status """ response = self.client.get(path=reverse("api_services_status"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_status(self): response = self.client.get( path=reverse("api_services_status", kwargs={"object_ptr": self.status.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_status(self): updates = {"name": "Api update", "details": "<p>api details</p>"} response = self.client.put( path=reverse("api_services_status", kwargs={"object_ptr": self.status.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_get_services_list(self): """ Test index page api/services """ response = self.client.get(path=reverse("api_services"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_service(self): response = self.client.get( path=reverse("api_services", kwargs={"object_ptr": self.service.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_service(self): updates = {"name": "Api update", "details": "<p>api details</p>"} response = self.client.put( path=reverse("api_services", kwargs={"object_ptr": self.service.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_get_sla_list(self): """ Test index page api/services/sla """ response = self.client.get(path=reverse("api_services_sla"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_sla(self): response = self.client.get( path=reverse("api_services_sla", kwargs={"object_ptr": self.sla.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_sla(self): updates = {"name": "Api update", "service": self.service.id, "provider": self.contact.id} response = self.client.put( path=reverse("api_services_sla", kwargs={"object_ptr": self.sla.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers ) print "content:" print response.content self.assertEquals(response.status_code, 200) def test_get_agents_list(self): """ Test index page api/services/agents """ response = self.client.get(path=reverse("api_services_agents"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_agent(self): response = self.client.get( path=reverse("api_services_agents", kwargs={"object_ptr": self.agent.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_sla(self): updates = {"activate": True, "related_user": User.objects.all()[0].id} response = self.client.put( path=reverse("api_services_agents", kwargs={"object_ptr": self.agent.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_get_queues_list(self): """ Test index page api/services/queues """ response = self.client.get(path=reverse("api_services_queues"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_queue(self): response = self.client.get( path=reverse("api_services_queues", kwargs={"object_ptr": self.queue.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_queue(self): updates = { "name": "Api test", "default_ticket_priority": 5, "ticket_code": "api", "waiting_time": 300, "default_ticket_status": self.status.id, } response = self.client.put( path=reverse("api_services_queues", kwargs={"object_ptr": self.queue.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_get_records(self): """ Test index page api/services/ticket/records/{ticket number} """ response = self.client.get( path=reverse("api_services_ticket_records", kwargs={"ticket_id": self.ticket.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_create_record(self): new_record = {"body": "api test message", "notify": False} response = self.client.post( path=reverse("api_services_ticket_records", kwargs={"ticket_id": self.ticket.id}), data=json.dumps(new_record), content_type=self.content_type, **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(new_record["body"], data["body"]) def test_get_tasks_list(self): """ Test index page api/services/tasks """ response = self.client.get(path=reverse("api_services_tickets"), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_ticket(self): response = self.client.get( path=reverse("api_services_tickets", kwargs={"object_ptr": self.ticket.id}), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_update_ticket(self): updates = {"name": "Api updates", "status": self.status.id, "priority": 3, "urgency": 5} response = self.client.put( path=reverse("api_services_tickets", kwargs={"object_ptr": self.ticket.id}), content_type=self.content_type, data=json.dumps(updates), **self.authentication_headers ) self.assertEquals(response.status_code, 200) def test_create_ticket(self): new_ticket = {"name": "Api creates", "status": self.status.id, "priority": 3, "urgency": 5} response = self.client.post( path=reverse("api_services_tickets") + "?" + urllib.urlencode({"queue_id": self.queue.id}), content_type=self.content_type, data=json.dumps(new_ticket), **self.authentication_headers ) self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertEquals(new_ticket["name"], data["name"]) self.assertEquals(new_ticket["urgency"], data["urgency"]) self.assertEquals(new_ticket["priority"], data["priority"]) self.assertEquals(new_ticket["status"], data["status"]["id"])
class SalesAPITest(TestCase): "Sales functional tests for views" 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() User.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.contact_type = ContactType() self.contact_type.slug = 'machine' self.contact_type.name = 'machine' self.contact_type.save() self.contact = Contact() self.contact.contact_type = self.contact_type self.contact.set_default_user() self.contact.save() self.assertNotEquals(self.contact.id, None) self.status = SaleStatus() self.status.active = True self.status.use_sales = True self.status.use_leads = True self.status.use_opportunities = True self.status.set_default_user() self.status.save() self.assertNotEquals(self.status.id, None) self.currency = Currency(code="GBP", name="Pounds", symbol="L", is_default=True) self.currency.save() self.source = SaleSource() self.source.active = True self.source.save() self.source.set_user(self.user) self.assertNotEquals(self.source.id, None) self.product = Product(name="Test") self.product.product_type = 'service' self.product.active = True self.product.sell_price = 10 self.product.buy_price = 100 self.product.set_default_user() self.product.save() self.assertNotEquals(self.product.id, None) self.subscription = Subscription() self.subscription.client = self.contact self.subscription.set_default_user() self.subscription.save() self.assertNotEquals(self.subscription.id, None) self.lead = Lead() self.lead.contact_method = 'email' self.lead.status = self.status self.lead.contact = self.contact self.lead.set_default_user() self.lead.save() self.assertNotEquals(self.lead.id, None) self.opportunity = Opportunity() self.opportunity.lead = self.lead self.opportunity.contact = self.contact self.opportunity.status = self.status self.opportunity.amount = 100 self.opportunity.amount_currency = self.currency self.opportunity.amount_display = 120 self.opportunity.set_default_user() self.opportunity.save() self.assertNotEquals(self.opportunity.id, None) self.order = SaleOrder(reference="Test") self.order.opportunity = self.opportunity self.order.status = self.status self.order.source = self.source self.order.currency = self.currency self.order.total = 0 self.order.total_display = 0 self.order.set_default_user() self.order.save() self.assertNotEquals(self.order.id, None) self.ordered_product = OrderedProduct() self.ordered_product.product = self.product self.ordered_product.order = self.order self.ordered_product.rate = 0 self.ordered_product.subscription = self.subscription self.ordered_product.set_default_user() self.ordered_product.save() self.assertNotEquals(self.ordered_product.id, None) self.client = Client() self.prepared = True def test_unauthenticated_access(self): "Test index page at /sales/statuses" response = self.client.get('/api/sales/statuses') # Redirects as unauthenticated self.assertEquals(response.status_code, 401) def test_get_statuses_list(self): """ Test index page api/sales/status """ response = self.client.get(path=reverse('api_sales_status'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_status(self): response = self.client.get(path=reverse('api_sales_status', kwargs={'object_ptr': self.status.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_status(self): updates = {"name": "Close_API", "active": True, "details": "api test details", "use_leads": True, "use_opportunities": True, "hidden": False} response = self.client.put(path=reverse('api_sales_status', 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['details'], updates['details']) self.assertEquals(data['use_leads'], updates['use_leads']) self.assertEquals(data['use_opportunities'], updates['use_opportunities']) self.assertEquals(data['hidden'], updates['hidden']) def test_get_products_list(self): """ Test index page api/sales/products """ response = self.client.get(path=reverse('api_sales_products'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_product(self): response = self.client.get(path=reverse('api_sales_products', kwargs={'object_ptr': self.product.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_product(self): updates = {"name": "API product", "parent": None, "product_type": "service", "code": "api_test_code", "buy_price": '100.05', "sell_price":'10.5', "active": True, "runout_action": "ignore", "details": "api details"} response = self.client.put(path=reverse('api_sales_products', kwargs={'object_ptr': self.product.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['product_type'], updates['product_type']) self.assertEquals(data['code'], updates['code']) self.assertEquals(data['buy_price'], updates['buy_price']) self.assertEquals(data['sell_price'], updates['sell_price']) self.assertEquals(data['active'], updates['active']) self.assertEquals(data['runout_action'], updates['runout_action']) self.assertEquals(data['details'], updates['details']) def test_get_sources_list(self): """ Test index page api/sales/sources """ response = self.client.get(path=reverse('api_sales_sources'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_source(self): response = self.client.get(path=reverse('api_sales_sources', kwargs={'object_ptr': self.source.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_source(self): updates = {"name":"Api source", "active": True, "details": "api details"} response = self.client.put(path=reverse('api_sales_sources', kwargs={'object_ptr': self.source.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['details'], updates['details']) # def test_get_leads_list(self): """ Test index page api/sales/leads """ response = self.client.get(path=reverse('api_sales_leads'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_lead(self): response = self.client.get(path=reverse('api_sales_leads', kwargs={'object_ptr': self.lead.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_lead(self): updates = {"status": self.status.id, "contact_method": "email", "contact": self.contact.id, "products_interested": [self.product.id], "source": self.source.id, 'details': 'Api details'} response = self.client.put(path=reverse('api_sales_leads', kwargs={'object_ptr': self.lead.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['status']['id'], updates['status']) self.assertEquals(data['contact_method'], updates['contact_method']) self.assertEquals(data['contact']['id'], updates['contact']) for i, product in enumerate(data['products_interested']): self.assertEquals(product['id'], updates['products_interested'][i]) self.assertEquals(data['source']['id'], updates['source']) self.assertEquals(data['details'], updates['details']) def test_get_opportunities_list(self): """ Test index page api/sales/opportunities """ response = self.client.get(path=reverse('api_sales_opportunities'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_opportunity(self): response = self.client.get(path=reverse('api_sales_opportunities', kwargs={'object_ptr': self.opportunity.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_opportunity(self): updates = {"status": self.status.id, "products_interested": [self.product.id], "contact": self.contact.id, "amount_display": 3000.56, "amount_currency": self.currency.id, "details": "API DETAILS"} response = self.client.put(path=reverse('api_sales_opportunities', kwargs={'object_ptr': self.opportunity.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['status']['id'], updates['status']) self.assertEquals(data['contact']['id'], updates['contact']) for i, product in enumerate(data['products_interested']): self.assertEquals(product['id'], updates['products_interested'][i]) self.assertEquals(data['amount_currency']['id'], updates['amount_currency']) self.assertEquals(data['details'], updates['details']) def test_get_orders_list(self): """ Test index page api/sales/orders """ response = self.client.get(path=reverse('api_sales_orders'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_order(self): response = self.client.get(path=reverse('api_sales_orders', kwargs={'object_ptr': self.order.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_order(self): updates = {"datetime": "2011-04-11 12:01:15", "status": self.status.id, "source": self.source.id, "details": "api details"} response = self.client.put(path=reverse('api_sales_orders', kwargs={'object_ptr': self.order.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['status']['id'], updates['status']) self.assertEquals(data['source']['id'], updates['source']) self.assertEquals(data['details'], updates['details']) def test_get_subscriptions_list(self): """ Test index page api/sales/subscriptions""" response = self.client.get(path=reverse('api_sales_subscriptions'), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_get_subscription(self): response = self.client.get(path=reverse('api_sales_subscriptions', kwargs={'object_ptr': self.subscription.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_subscription(self): updates = {"product": self.product.id, "start": "2011-06-30", "cycle_period": "daily", "active": True, "details": "api details"} response = self.client.put(path=reverse('api_sales_subscriptions', kwargs={'object_ptr': self.subscription.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['product']['id'], updates['product']) self.assertEquals(data['cycle_period'], updates['cycle_period']) self.assertEquals(data['active'], updates['active']) self.assertEquals(data['details'], updates['details']) def test_get_ordered_product(self): response = self.client.get(path=reverse('api_sales_ordered_products', kwargs={'object_ptr': self.ordered_product.id}), **self.authentication_headers) self.assertEquals(response.status_code, 200) def test_update_ordered_product(self): updates = {"discount": '10.0', "product": self.product.id, "quantity": '10'} response = self.client.put(path=reverse('api_sales_ordered_products', kwargs={'object_ptr': self.ordered_product.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['product']['id'], updates['product']) self.assertEquals(data['discount'], updates['discount']) self.assertEquals(data['quantity'], updates['quantity'])