def setUp(self): super(ReadProviderTest, self).setUp() self.p1 = Provider(name="prov1", provider_type="DUMMY", access_key="keyzz") self.p1.save() self.p2 = Provider(name="prov2", provider_type="DUMMY", access_key="keyzz2") self.p2.save() self.p3 = Provider(name="prov3", provider_type="dedicated") self.p3.save()
class ReadProviderTest(BaseProviderTestCase): def setUp(self): super(ReadProviderTest, self).setUp() self.p1 = Provider(name="prov1", provider_type="DUMMY", access_key="keyzz") self.p1.save() self.p2 = Provider(name="prov2", provider_type="DUMMY", access_key="keyzz2") self.p2.save() self.p3 = Provider(name="prov3", provider_type="dedicated") self.p3.save() def test_not_authenticated(self): '''A non authenticated user should get 401''' # NOTE: Use non-authenticated client response = self.client.get(self.path) self.assertEquals(response.status_code, 401) def test_get_all_providers(self): '''Should show all existing providers''' response = self.client.get(self.path) self.assertEquals(response.status_code, 200) expected = [ {'id': self.p1.id, 'access_key': self.p1.access_key, 'provider_type': self.p1.provider_type, 'name': self.p1.name}, {'id': self.p2.id, 'access_key': self.p2.access_key, 'provider_type': self.p2.provider_type, 'name': self.p2.name}, {'id': self.p3.id, 'access_key': self.p3.access_key, 'provider_type': self.p3.provider_type, 'name': self.p3.name}, ] self.assertEquals(json.loads(response.content), expected) def test_get_providers_by_type_dummy(self): '''Should show all providers of type DUMMY''' response = self.client.get(self.path + "?provider_type=DUMMY") self.assertEquals(response.status_code, 200) expected = [ {'id': self.p1.id, 'access_key': self.p1.access_key, 'provider_type': self.p1.provider_type, 'name': self.p1.name}, {'id': self.p2.id, 'access_key': self.p2.access_key, 'provider_type': self.p2.provider_type, 'name': self.p2.name}, ] self.assertEquals(json.loads(response.content), expected) def test_get_providers_by_type_dedicated(self): '''Should show all providers of type dedicated''' response = self.client.get(self.path + "?provider_type=dedicated") self.assertEquals(response.status_code, 200) expected = [ {'id': self.p3.id, 'access_key': self.p3.access_key, 'provider_type': self.p3.provider_type, 'name': self.p3.name}, ] self.assertEquals(json.loads(response.content), expected) def test_get_providers_by_type_not_found(self): '''Should show providers for non-existent type''' response = self.client.get(self.path + "?provider_type=DUMMIEST") self.assertEquals(response.status_code, 200) expected = [] self.assertEquals(json.loads(response.content), expected) def test_get_provider_by_id(self): '''Should show provider with id=2''' response = self.client.get(self.path + "2") self.assertEquals(response.status_code, 200) expected = { 'id': self.p2.id, 'access_key': self.p2.access_key, 'provider_type': self.p2.provider_type, 'name': self.p2.name, } self.assertEquals(json.loads(response.content), expected) def test_get_provider_by_id_not_found(self): '''Should return NOT_FOUND when requesting a provider with non existing id''' response = self.client.get(self.path + '99999') self.assertEquals(response.status_code, 404) def test_get_provider_by_name(self): '''Should show provider with name "prov1"''' response = self.client.get(self.path + "?name=prov1") self.assertEquals(response.status_code, 200) expected = { 'id': self.p1.id, 'access_key': self.p1.access_key, 'provider_type': self.p1.provider_type, 'name': self.p1.name } self.assertEquals(json.loads(response.content), expected) def test_get_provider_by_name_not_found(self): '''Should return NOT_FOUND when requesting a provider with a non existing name''' response = self.client.get(self.path + "?name=prov1nothere") self.assertEquals(response.status_code, 404)