Esempio n. 1
0
    def test_non_owner(self):
        new_client = AuthorizedClient()

        assert new_client.get(self.url + '?keys=value1').status_code in (
            status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN)

        assert new_client.post(self.url,
                               data={
                                   node_selectors.NODE_SELECTORS_BUILD_JOBS: {
                                       'foo': 'bar'
                                   }
                               }).status_code in (status.HTTP_401_UNAUTHORIZED,
                                                  status.HTTP_403_FORBIDDEN)
Esempio n. 2
0
class TestK8SResourceListViewV1(BaseViewTest):
    serializer_class = None
    model_class = None
    factory_class = None
    num_objects = 3
    HAS_AUTH = True
    ADMIN_USER = True

    def setUp(self):
        super().setUp()
        self.normal_client = AuthorizedClient()
        self.url = self.get_url()
        self.objects = [self.factory_class()  # pylint:disable=not-callable
                        for _ in range(self.num_objects)]
        self.queryset = self.model_class.objects.filter()

    def get_url(self):
        return ''

    def test_get(self):
        resp = self.auth_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK

        assert resp.data['next'] is None
        assert resp.data['count'] == len(self.objects)

        data = resp.data['results']
        assert len(data) == self.queryset.count()
        for i in data:
            assert i in self.serializer_class(self.queryset, many=True).data

        # Non admin
        resp = self.normal_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK

        assert resp.data['next'] is None
        assert resp.data['count'] == len(self.objects)

        data = resp.data['results']
        assert len(data) == self.queryset.count()
        for i in data:
            assert i in self.serializer_class(self.queryset, many=True).data

    def test_pagination(self):
        limit = self.num_objects - 1
        resp = self.auth_client.get("{}?limit={}".format(self.url, limit))
        assert resp.status_code == status.HTTP_200_OK

        next_page = resp.data.get('next')
        assert next_page is not None
        assert resp.data['count'] == self.queryset.count()

        data = resp.data['results']
        assert len(data) == limit
        query_data = self.serializer_class(self.queryset, many=True).data
        for i in data:
            assert i in query_data

        resp = self.auth_client.get(next_page)
        assert resp.status_code == status.HTTP_200_OK

        assert resp.data['next'] is None

        data = resp.data['results']
        assert len(data) == 1
        for i in data:
            assert i in query_data

    def test_create(self):
        data = {}
        resp = self.auth_client.post(self.url, data)
        assert resp.status_code == status.HTTP_400_BAD_REQUEST

        data = {
            'name': 'new_config',
            'description': 'some description',
            'k8s_ref': 'k8s_resource',
            'keys': ['key1', 'key2'],
            'tags': ['foo', 'bar']
        }
        resp = self.auth_client.post(self.url, data)
        assert resp.status_code == status.HTTP_201_CREATED
        assert self.model_class.objects.count() == self.num_objects + 1
        last_object = self.model_class.objects.last()
        assert last_object.owner.owner == Cluster.load()
        assert last_object.name == data['name']
        assert last_object.description == data['description']
        assert last_object.k8s_ref == data['k8s_ref']
        assert last_object.keys == data['keys']
        assert last_object.tags == data['tags']

        # Non admin
        data = {}
        resp = self.normal_client.post(self.url, data)
        assert resp.status_code == status.HTTP_403_FORBIDDEN

        data = {
            'name': 'new_config',
            'description': 'some description',
            'k8s_ref': 'k8s_ref1',
            'keys': ['key1', 'key2'],
            'tags': ['foo', 'bar']
        }
        resp = self.normal_client.post(self.url, data)
        assert resp.status_code == status.HTTP_403_FORBIDDEN
Esempio n. 3
0
class TestK8SResourceDetailViewV1(BaseViewTest):
    serializer_class = None
    model_class = None
    factory_class = None
    HAS_AUTH = True
    ADMIN_USER = True

    def setUp(self):
        super().setUp()
        self.normal_client = AuthorizedClient()
        self.object = self.factory_class()  # pylint:disable=not-callable
        self.url = self.get_url()
        self.queryset = self.model_class.objects.all()

    def get_url(self):
        return ''

    def test_get(self):
        resp = self.auth_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK
        assert resp.data == self.serializer_class(self.object).data

        # Non admin can get
        resp = self.normal_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK
        assert resp.data == self.serializer_class(self.object).data

    def test_patch(self):
        data = {
            'name': 'foo',
            'description': 'new description',
            'tags': ['foo', 'bar'],
            'k8s_ref': 'new_ref',
            'keys': ['key1', 'key2'],
        }
        assert self.object.name != data['name']
        assert self.object.description != data['description']
        assert self.object.tags != data['tags']
        assert self.object.k8s_ref != data['k8s_ref']
        assert self.object.keys != data['keys']

        resp = self.auth_client.patch(self.url, data=data)
        assert resp.status_code == status.HTTP_200_OK
        new_object = self.model_class.objects.get(id=self.object.id)
        assert new_object.name == data['name']
        assert new_object.description == data['description']
        assert new_object.tags == data['tags']
        assert new_object.k8s_ref == data['k8s_ref']
        assert new_object.keys == data['keys']

        # Non admin
        resp = self.normal_client.patch(self.url, data=data)
        assert resp.status_code == status.HTTP_403_FORBIDDEN

    def test_delete(self):
        # Non admin
        resp = self.normal_client.delete(self.url)
        assert resp.status_code == status.HTTP_403_FORBIDDEN

        assert self.model_class.objects.count() == 1
        resp = self.auth_client.delete(self.url)
        assert resp.status_code == status.HTTP_204_NO_CONTENT
        assert self.model_class.objects.count() == 0
Esempio n. 4
0
class TestRegistryAccessListViewV1(BaseViewTest):
    serializer_class = RegistryAccessSerializer
    model_class = RegistryAccess
    factory_class = RegistryAccessFactory
    num_objects = 3
    HAS_AUTH = True
    ADMIN_USER = True

    def setUp(self):
        super().setUp()
        self.normal_client = AuthorizedClient()
        self.url = '/{}/catalogs/registry_access/'.format(API_V1)
        self.objects = [self.factory_class() for _ in range(self.num_objects)]
        self.queryset = self.model_class.objects.filter()

    def test_get(self):
        resp = self.auth_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK

        assert resp.data['next'] is None
        assert resp.data['count'] == len(self.objects)

        data = resp.data['results']
        assert len(data) == self.queryset.count()
        for i in data:
            assert i in self.serializer_class(self.queryset, many=True).data

        # Non admin
        resp = self.normal_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK

        assert resp.data['next'] is None
        assert resp.data['count'] == len(self.objects)

        data = resp.data['results']
        assert len(data) == self.queryset.count()
        for i in data:
            assert i in self.serializer_class(self.queryset, many=True).data

    def test_pagination(self):
        limit = self.num_objects - 1
        resp = self.auth_client.get("{}?limit={}".format(self.url, limit))
        assert resp.status_code == status.HTTP_200_OK

        next_page = resp.data.get('next')
        assert next_page is not None
        assert resp.data['count'] == self.queryset.count()

        data = resp.data['results']
        assert len(data) == limit
        query_data = self.serializer_class(self.queryset, many=True).data
        for i in data:
            assert i in query_data

        resp = self.auth_client.get(next_page)
        assert resp.status_code == status.HTTP_200_OK

        assert resp.data['next'] is None

        data = resp.data['results']
        assert len(data) == 1
        for i in data:
            assert i in query_data

    def test_create(self):

        data = {}
        resp = self.auth_client.post(self.url, data)
        assert resp.status_code == status.HTTP_400_BAD_REQUEST

        secret = K8SSecretFactory()

        data = {
            'name': 'foo',
            'description': 'new description',
            'tags': ['foo', 'bar'],
            'host': 'https://foo.bar',
            'k8s_secret': secret.id,
        }
        resp = self.auth_client.post(self.url, data)
        assert resp.status_code == status.HTTP_201_CREATED
        assert self.model_class.objects.count() == self.num_objects + 1
        last_object = self.model_class.objects.last()
        assert last_object.owner.owner == Cluster.load()
        assert last_object.name == data['name']
        assert last_object.description == data['description']
        assert last_object.tags == data['tags']
        assert last_object.host == data['host']
        assert last_object.k8s_secret.id == data['k8s_secret']

        # Non admin
        data = {}
        resp = self.normal_client.post(self.url, data)
        assert resp.status_code == status.HTTP_403_FORBIDDEN

        data = {
            'name': 'foo',
            'description': 'new description',
            'tags': ['foo', 'bar'],
            'host': 'https:foo.bar',
            'k8s_secret': secret.id,
        }
        resp = self.normal_client.post(self.url, data)
        assert resp.status_code == status.HTTP_403_FORBIDDEN
Esempio n. 5
0
class TestRegistryAccessDetailViewV1(BaseViewTest):
    serializer_class = RegistryAccessSerializer
    model_class = RegistryAccess
    factory_class = RegistryAccessFactory
    HAS_AUTH = True
    ADMIN_USER = True

    def setUp(self):
        super().setUp()
        self.normal_client = AuthorizedClient()
        self.object = self.factory_class()
        self.url = '/{}/catalogs/registry_access/{}/'.format(API_V1, self.object.name)
        self.queryset = self.model_class.objects.all()

    def test_get(self):
        resp = self.auth_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK
        assert resp.data == self.serializer_class(self.object).data

        # Non admin can get
        resp = self.normal_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK
        assert resp.data == self.serializer_class(self.object).data

    def test_patch(self):
        secret = K8SSecretFactory()

        data = {
            'name': 'foo',
            'description': 'new description',
            'tags': ['foo', 'bar'],
            'host': 'https://foo.bar',
            'k8s_secret': secret.id,
        }
        assert self.object.name != data['name']
        assert self.object.description != data['description']
        assert self.object.tags != data['tags']
        assert self.object.host != data['host']
        assert self.object.k8s_secret != data['k8s_secret']

        resp = self.auth_client.patch(self.url, data=data)
        assert resp.status_code == status.HTTP_200_OK
        new_object = self.model_class.objects.get(id=self.object.id)
        assert new_object.name == data['name']
        assert new_object.description == data['description']
        assert set(new_object.tags) == set(data['tags'])
        assert new_object.host == data['host']
        assert new_object.k8s_secret.id == data['k8s_secret']

        # Non admin
        resp = self.normal_client.patch(self.url, data=data)
        assert resp.status_code == status.HTTP_403_FORBIDDEN

    def test_delete(self):
        # Non admin
        resp = self.normal_client.delete(self.url)
        assert resp.status_code == status.HTTP_403_FORBIDDEN

        assert self.model_class.objects.count() == 1
        resp = self.auth_client.delete(self.url)
        assert resp.status_code == status.HTTP_204_NO_CONTENT
        assert self.model_class.objects.count() == 0
Esempio n. 6
0
class TestDataStoreDetailViewV1(BaseViewTest):
    serializer_class = DataStoreSerializer
    model_class = DataStore
    factory_class = DataStoreFactory
    HAS_AUTH = True
    ADMIN_USER = True

    def setUp(self):
        super().setUp()
        self.normal_client = AuthorizedClient()
        self.object = self.factory_class()
        self.url = '/{}/catalogs/data_stores/{}/'.format(
            API_V1, self.object.name)
        self.queryset = self.model_class.objects.all()

    def test_get(self):
        resp = self.auth_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK
        assert resp.data == self.serializer_class(self.object).data

        # Non admin can get
        resp = self.normal_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK
        assert resp.data == self.serializer_class(self.object).data

    def test_patch(self):
        secret = K8SSecretFactory()

        data = {
            'name': 'foo',
            'description': 'new description',
            'tags': ['foo', 'bar'],
            'type': StoreTypes.S3,
            'bucket': 'foo',
            'k8s_secret': secret.id,
            'read_only': True
        }
        assert self.object.name != data['name']
        assert self.object.description != data['description']
        assert self.object.tags != data['tags']
        assert self.object.type != data['type']
        assert self.object.bucket != data['bucket']
        assert self.object.k8s_secret != data['k8s_secret']
        assert self.object.read_only is False

        resp = self.auth_client.patch(self.url, data=data)
        assert resp.status_code == status.HTTP_200_OK
        new_object = self.model_class.objects.get(id=self.object.id)
        assert new_object.name == data['name']
        assert new_object.description == data['description']
        assert set(new_object.tags) == set(data['tags'])
        assert new_object.type == data['type']
        assert new_object.bucket == data['bucket']
        assert new_object.k8s_secret.id == data['k8s_secret']
        assert new_object.read_only is True

        # Non admin
        resp = self.normal_client.patch(self.url, data=data)
        assert resp.status_code == status.HTTP_403_FORBIDDEN

    def test_delete(self):
        # Non admin
        resp = self.normal_client.delete(self.url)
        assert resp.status_code == status.HTTP_403_FORBIDDEN

        assert self.model_class.objects.count() == 1
        resp = self.auth_client.delete(self.url)
        assert resp.status_code == status.HTTP_204_NO_CONTENT
        assert self.model_class.objects.count() == 0
Esempio n. 7
0
class TestRegistryAccessDetailViewV1(BaseViewTest):
    serializer_class = RegistryAccessSerializer
    model_class = RegistryAccess
    factory_class = RegistryAccessFactory
    HAS_AUTH = True
    ADMIN_USER = True

    def setUp(self):
        super().setUp()
        self.normal_client = AuthorizedClient()
        self.object = self.factory_class()
        self.url = self.get_access_url(self.object.name)
        self.queryset = self.model_class.objects.all()

    @staticmethod
    def get_access_url(name):
        return '/{}/catalogs/registry_access/{}/'.format(API_V1, name)

    def test_get(self):
        resp = self.auth_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK
        assert resp.data == self.serializer_class(self.object).data

        # Non admin can get
        resp = self.normal_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK
        assert resp.data == self.serializer_class(self.object).data

    def test_patch(self):
        secret = K8SSecretFactory()

        data = {
            'name': 'foo',
            'description': 'new description',
            'tags': ['foo', 'bar'],
            'host': 'https://foo.bar',
            'k8s_secret': secret.id,
        }
        assert self.object.name != data['name']
        assert self.object.description != data['description']
        assert self.object.tags != data['tags']
        assert self.object.host != data['host']
        assert self.object.k8s_secret != data['k8s_secret']

        resp = self.auth_client.patch(self.url, data=data)
        assert resp.status_code == status.HTTP_200_OK
        new_object = self.model_class.objects.get(id=self.object.id)
        assert new_object.name == data['name']
        assert new_object.description == data['description']
        assert set(new_object.tags) == set(data['tags'])
        assert new_object.host == data['host']
        assert new_object.k8s_secret.id == data['k8s_secret']

        # Path an access without a host
        new_secret = K8SSecretFactory()
        last_object = RegistryAccess.objects.create(owner=self.object.owner,
                                                    name='my_registry',
                                                    k8s_secret=secret)
        assert last_object.owner.owner == Cluster.load()
        assert last_object.name == 'my_registry'
        assert last_object.description is None
        assert last_object.tags is None
        assert last_object.host == ''
        assert last_object.k8s_secret.id == data['k8s_secret']
        url = self.get_access_url(last_object.name)

        # Patch
        data = {
            'k8s_secret': new_secret.id,
        }
        resp = self.auth_client.patch(url, data=data)
        assert resp.status_code == status.HTTP_200_OK
        new_object = self.model_class.objects.get(id=last_object.id)
        assert new_object.name == 'my_registry'
        assert new_object.description is None
        assert new_object.tags is None
        assert new_object.host == ''
        assert new_object.k8s_secret.id == data['k8s_secret']

        # Patch
        data = {
            'tags': ['foo', 'bar'],
        }
        resp = self.auth_client.patch(url, data=data)
        assert resp.status_code == status.HTTP_200_OK
        new_object = self.model_class.objects.get(id=last_object.id)
        assert new_object.name == 'my_registry'
        assert new_object.description is None
        assert set(new_object.tags) == set(data['tags'])
        assert new_object.host == ''
        assert new_object.k8s_secret.id == new_secret.id

        # Patch
        data = {'tags': ['foo', 'bar'], 'host': ''}
        resp = self.auth_client.patch(url, data=data)
        assert resp.status_code == status.HTTP_200_OK
        new_object = self.model_class.objects.get(id=last_object.id)
        assert new_object.name == 'my_registry'
        assert new_object.description is None
        assert set(new_object.tags) == set(data['tags'])
        assert new_object.host == ''
        assert new_object.k8s_secret.id == new_secret.id

        # Non admin
        resp = self.normal_client.patch(self.url, data=data)
        assert resp.status_code == status.HTTP_403_FORBIDDEN

    def test_delete(self):
        # Non admin
        resp = self.normal_client.delete(self.url)
        assert resp.status_code == status.HTTP_403_FORBIDDEN

        assert self.model_class.objects.count() == 1
        resp = self.auth_client.delete(self.url)
        assert resp.status_code == status.HTTP_204_NO_CONTENT
        assert self.model_class.objects.count() == 0