class FinanceAPITest(TestCase): """Finance api tests""" username = "******" password = "******" authentication_headers = { "CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk" } content_type = 'application/json' def setUp(self): self.group, created = Group.objects.get_or_create(name='test') self.user, created = DjangoUser.objects.get_or_create( username=self.username) self.user.set_password(self.password) self.user.save() self.perspective = Perspective(name='test') self.perspective.set_default_user() self.perspective.save() ModuleSetting.set('default_perspective', self.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() 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) 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.contact.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_2(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, "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) 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'].replace("T", " "), updates['datetime']) self.assertEquals(data['target']['id'], updates['target']) self.assertEquals(data['account']['id'], updates['account']) self.assertEquals(data['source']['id'], updates['source'])
class InfrastructureApiTest(TestCase): """Infrastructure functional tests for api""" username = "******" password = "******" authentication_headers = {"CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk"} content_type = 'application/json' def setUp(self): self.group, created = Group.objects.get_or_create(name='test') self.user, created = DjangoUser.objects.get_or_create(username=self.username) self.user.set_password(self.password) self.user.save() self.perspective = Perspective(name='test') self.perspective.set_default_user() self.perspective.save() ModuleSetting.set('default_perspective', self.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() 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) 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 FinanceAPITest(TestCase): """Finance api tests""" username = "******" password = "******" authentication_headers = {"CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk"} content_type = 'application/json' def setUp(self): self.group, created = Group.objects.get_or_create(name='test') self.user, created = DjangoUser.objects.get_or_create(username=self.username) self.user.set_password(self.password) self.user.save() self.perspective = Perspective(name='test') self.perspective.set_default_user() self.perspective.save() ModuleSetting.set('default_perspective', self.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() 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) 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.contact.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_2(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, "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) 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'].replace("T", " "), updates['datetime']) self.assertEquals(data['target']['id'], updates['target']) self.assertEquals(data['account']['id'], updates['account']) self.assertEquals(data['source']['id'], updates['source'])
class CoreAPITest(TestCase): """Core api tests""" username = "******" password = "******" authentication_headers = {"CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk"} content_type = 'application/json' def setUp(self): 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 = duser 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() 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) 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) 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.profile.id}), **self.authentication_headers) 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.profile.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): username = "******" password = "******" authentication_headers = { "CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk" } content_type = 'application/json' def setUp(self): self.group, created = Group.objects.get_or_create(name='test') self.user, created = DjangoUser.objects.get_or_create( username=self.username) self.user.set_password(self.password) self.user.save() self.perspective = Perspective(name='test') self.perspective.set_default_user() self.perspective.save() ModuleSetting.set('default_perspective', self.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.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() 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) 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_agents(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 MessagingApiTest(TestCase): username = "******" password = "******" authentication_headers = {"CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk"} content_type = 'application/json' def setUp(self): self.group, created = Group.objects.get_or_create(name='test') self.user, created = DjangoUser.objects.get_or_create(username=self.username) self.user.set_password(self.password) self.user.save() self.perspective = Perspective(name='test') self.perspective.set_default_user() self.perspective.save() ModuleSetting.set('default_perspective', self.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.profile, contact_type=self.contact_type) self.user_contact.set_user(self.user.profile) 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() 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 SalesAPITest(TestCase): username = "******" password = "******" authentication_headers = {"CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk"} content_type = 'application/json' def setUp(self): self.group, created = Group.objects.get_or_create(name='test') self.user, created = DjangoUser.objects.get_or_create(username=self.username) self.user.set_password(self.password) self.user.save() self.perspective = Perspective(name='test') self.perspective.set_default_user() self.perspective.save() ModuleSetting.set('default_perspective', self.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.profile) 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) 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'])
class ServicesViewsTest(TestCase): username = "******" password = "******" authentication_headers = { "CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk", } content_type = "application/json" def setUp(self): self.group, created = Group.objects.get_or_create(name="test") self.user, created = DjangoUser.objects.get_or_create(username=self.username) self.user.set_password(self.password) self.user.save() self.perspective = Perspective(name="test") self.perspective.set_default_user() self.perspective.save() ModuleSetting.set("default_perspective", self.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.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() 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 ) 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_agents(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 CoreAPITest(TestCase): """Core api tests""" username = "******" password = "******" authentication_headers = { "CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk" } content_type = 'application/json' def setUp(self): 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 = duser 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() 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) 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) 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.profile.id}), **self.authentication_headers) 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.profile.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 KnowledgeViewsTest(TestCase): username = "******" password = "******" authentication_headers = { "CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk" } content_type = 'application/json' def setUp(self): self.group, created = Group.objects.get_or_create(name='test') self.user, created = DjangoUser.objects.get_or_create( username=self.username) self.user.set_password(self.password) self.user.save() self.perspective = Perspective(name='test') self.perspective.set_default_user() self.perspective.save() ModuleSetting.set('default_perspective', self.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() 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, 200) data = json.loads(response.content) self.assertEquals(updates["name"], data["name"]) self.assertEquals(updates["body"], data["body"]) self.assertEquals(updates["folder"], data["folder"]["id"]) self.assertEquals(updates["category"], data["category"]["id"])
class KnowledgeViewsTest(TestCase): username = "******" password = "******" authentication_headers = {"CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk"} content_type = 'application/json' def setUp(self): self.group, created = Group.objects.get_or_create(name='test') self.user, created = DjangoUser.objects.get_or_create(username=self.username) self.user.set_password(self.password) self.user.save() self.perspective = Perspective(name='test') self.perspective.set_default_user() self.perspective.save() ModuleSetting.set('default_perspective', self.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() 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, 200) data = json.loads(response.content) self.assertEquals(updates["name"], data["name"]) self.assertEquals(updates["body"], data["body"]) self.assertEquals(updates["folder"], data["folder"]["id"]) self.assertEquals(updates["category"], data["category"]["id"])
class InfrastructureApiTest(TestCase): """Infrastructure functional tests for api""" username = "******" password = "******" authentication_headers = { "CONTENT_TYPE": "application/json", "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk" } content_type = 'application/json' def setUp(self): self.group, created = Group.objects.get_or_create(name='test') self.user, created = DjangoUser.objects.get_or_create( username=self.username) self.user.set_password(self.password) self.user.save() self.perspective = Perspective(name='test') self.perspective.set_default_user() self.perspective.save() ModuleSetting.set('default_perspective', self.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() 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) 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'])