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 setUp(self):
     super(ReadImageTest, self).setUp()
     
     self.p1 = Provider(name="prov1", provider_type="DUMMY", access_key="keyzz")
     self.p1.save()
     self.p1.import_images()
     self.p2 = Provider(name="prov2", provider_type="DUMMY", access_key="keyzz2")
     self.p2.save()
     self.p2.import_images()
    def setUp(self):
        super(ReadImageTest, self).setUp()

        self.p1 = Provider(name="prov1",
                           provider_type="DUMMY",
                           access_key="keyzz")
        self.p1.save()
        self.p1.import_images()
        self.p2 = Provider(name="prov2",
                           provider_type="DUMMY",
                           access_key="keyzz2")
        self.p2.save()
        self.p2.import_images()
class CreateImageTest(BaseProviderTestCase):
    def setUp(self):
        super(ReadImageTest, self).setUp()

        self.p1 = Provider(name="prov1",
                           provider_type="DUMMY",
                           access_key="keyzz")
        self.p1.save()
        self.p1.import_images()
        self.p2 = Provider(name="prov2",
                           provider_type="DUMMY",
                           access_key="keyzz2")
        self.p2.save()
        self.p2.import_images()

    def test_create_image_should_fail(self):
        '''Should return not allowed when trying to POST'''
        data = {
            "image_id": "10",
            "name": "myimage",
            "favorite": False,
            "provider_id": "1"
        }
        resp = self.client.post(self.path,
                                json.dumps(data),
                                content_type='application/json')
        self.assertEquals(response.status_code, 405)
class ReadImageTest(BaseProviderTestCase):
    def setUp(self):
        super(ReadImageTest, self).setUp()
        
        self.p1 = Provider(name="prov1", provider_type="DUMMY", access_key="keyzz")
        self.p1.save()
        self.p1.import_images()
        self.p2 = Provider(name="prov2", provider_type="DUMMY", access_key="keyzz2")
        self.p2.save()
        self.p2.import_images()
    
    def test_get_all_images(self):
        '''Should return all images for a given provider'''
        response = self.client.get(self.path + str(self.p1.id) + "/images/")
        self.assertEquals(response.status_code, 200)
        expected = [
            {"id": 1, "image_id": "1", "name": "Ubuntu 9.10", "favorite": False},
            {"id": 2,"image_id": "2","name": "Ubuntu 9.04", "favorite": False},
            {"id": 3, "image_id": "3", "name": "Slackware 4", "favorite": False},
        ]
        self.assertEquals(json.loads(response.content), expected)
    
    def test_get_image_by_id(self):
        '''Should show image with id=2'''
        response = self.client.get(self.path + str(self.p1.id) + "/images/2")
        self.assertEquals(response.status_code, 200)
        expected = {
            "id": 2,"image_id": "2","name": "Ubuntu 9.04", "favorite": False}
        self.assertEquals(json.loads(response.content), expected)
    
    def test_get_image_by_image_id(self):
        '''Should show image with image_id=2'''
        path = self.path + str(self.p1.id) + "/images/" + "?image_id=2"
        response = self.client.get(path)
        self.assertEquals(response.status_code, 200)
        expected = {
            "id": 2,"image_id": "2","name": "Ubuntu 9.04", "favorite": False}
        self.assertEquals(json.loads(response.content), expected)
    
    def test_get_image_by_name(self):
        '''Should show image with name=Ubuntu 9.04'''
        path = self.path + str(self.p1.id) + "/images/" + "?name=Ubuntu 9.04"
        response = self.client.get(path)
        self.assertEquals(response.status_code, 200)
        expected = {
            "id": 2,"image_id": "2","name": "Ubuntu 9.04", "favorite": False}
        self.assertEquals(json.loads(response.content), expected)
class CreateImageTest(BaseProviderTestCase):
    def setUp(self):
        super(ReadImageTest, self).setUp()
        
        self.p1 = Provider(name="prov1", provider_type="DUMMY", access_key="keyzz")
        self.p1.save()
        self.p1.import_images()
        self.p2 = Provider(name="prov2", provider_type="DUMMY", access_key="keyzz2")
        self.p2.save()
        self.p2.import_images()
    
    def test_create_image_should_fail(self):
        '''Should return not allowed when trying to POST'''
        data = {"image_id": "10", "name": "myimage", "favorite": False,
            "provider_id": "1"}
        resp = self.client.post(
            self.path, json.dumps(data), content_type='application/json')
        self.assertEquals(response.status_code, 405)
    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):
        """Should get a 401 when the user is not authenticated"""
        # 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)
class ReadImageTest(BaseProviderTestCase):
    def setUp(self):
        super(ReadImageTest, self).setUp()

        self.p1 = Provider(name="prov1",
                           provider_type="DUMMY",
                           access_key="keyzz")
        self.p1.save()
        self.p1.import_images()
        self.p2 = Provider(name="prov2",
                           provider_type="DUMMY",
                           access_key="keyzz2")
        self.p2.save()
        self.p2.import_images()

    def test_get_all_images(self):
        '''Should return all images for a given provider'''
        response = self.client.get(self.path + str(self.p1.id) + "/images/")
        self.assertEquals(response.status_code, 200)
        expected = [
            {
                "id": 1,
                "image_id": "1",
                "name": "Ubuntu 9.10",
                "favorite": False
            },
            {
                "id": 2,
                "image_id": "2",
                "name": "Ubuntu 9.04",
                "favorite": False
            },
            {
                "id": 3,
                "image_id": "3",
                "name": "Slackware 4",
                "favorite": False
            },
        ]
        self.assertEquals(json.loads(response.content), expected)

    def test_get_image_by_id(self):
        '''Should show image with id=2'''
        response = self.client.get(self.path + str(self.p1.id) + "/images/2")
        self.assertEquals(response.status_code, 200)
        expected = {
            "id": 2,
            "image_id": "2",
            "name": "Ubuntu 9.04",
            "favorite": False
        }
        self.assertEquals(json.loads(response.content), expected)

    def test_get_image_by_image_id(self):
        '''Should show image with image_id=2'''
        path = self.path + str(self.p1.id) + "/images/" + "?image_id=2"
        response = self.client.get(path)
        self.assertEquals(response.status_code, 200)
        expected = {
            "id": 2,
            "image_id": "2",
            "name": "Ubuntu 9.04",
            "favorite": False
        }
        self.assertEquals(json.loads(response.content), expected)

    def test_get_image_by_name(self):
        '''Should show image with name=Ubuntu 9.04'''
        path = self.path + str(self.p1.id) + "/images/" + "?name=Ubuntu 9.04"
        response = self.client.get(path)
        self.assertEquals(response.status_code, 200)
        expected = {
            "id": 2,
            "image_id": "2",
            "name": "Ubuntu 9.04",
            "favorite": False
        }
        self.assertEquals(json.loads(response.content), expected)
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):
        """Should get a 401 when the user is not authenticated"""
        # 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)