Exemple #1
0
class TestNodeForkCreate(ApiTestCase):
    def setUp(self):
        super(TestNodeForkCreate, self).setUp()
        self.user = AuthUserFactory()
        self.user_two = AuthUserFactory()
        self.user_three = AuthUserFactory()
        self.private_project = ProjectFactory(creator=self.user)

        self.fork_data = {'data': {'type': 'nodes'}}

        self.fork_data_with_title = {
            'data': {
                'type': 'nodes',
                'attributes': {
                    'title': 'My Forked Project'
                }
            }
        }

        self.private_project_url = '/{}nodes/{}/forks/'.format(
            API_BASE, self.private_project._id)

        self.public_project = ProjectFactory(is_public=True, creator=self.user)
        self.public_project_url = '/{}nodes/{}/forks/'.format(
            API_BASE, self.public_project._id)

    def tearDown(self):
        super(TestNodeForkCreate, self).tearDown()
        Node.remove()

    def test_create_fork_from_public_project_with_new_title(self):
        res = self.app.post_json_api(self.public_project_url,
                                     self.fork_data_with_title,
                                     auth=self.user.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['id'], self.public_project.forks[0]._id)
        assert_equal(res.json['data']['attributes']['title'],
                     self.fork_data_with_title['data']['attributes']['title'])

    def test_create_fork_from_private_project_with_new_title(self):
        res = self.app.post_json_api(self.private_project_url,
                                     self.fork_data_with_title,
                                     auth=self.user.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['id'], self.private_project.forks[0]._id)
        assert_equal(res.json['data']['attributes']['title'],
                     self.fork_data_with_title['data']['attributes']['title'])

    def test_can_fork_public_node_logged_in(self):
        res = self.app.post_json_api(self.public_project_url,
                                     self.fork_data,
                                     auth=self.user_two.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['id'], self.public_project.forks[0]._id)
        assert_equal(res.json['data']['attributes']['title'],
                     'Fork of ' + self.public_project.title)

    def test_cannot_fork_public_node_logged_out(self):
        res = self.app.post_json_api(self.public_project_url,
                                     self.fork_data,
                                     expect_errors=True)
        assert_equal(res.status_code, 401)
        assert_equal(res.json['errors'][0]['detail'],
                     'Authentication credentials were not provided.')

    def test_can_fork_public_node_logged_in_contributor(self):
        res = self.app.post_json_api(self.public_project_url,
                                     self.fork_data,
                                     auth=self.user.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['id'], self.public_project.forks[0]._id)
        assert_equal(res.json['data']['attributes']['title'],
                     'Fork of ' + self.public_project.title)

    def test_cannot_fork_private_node_logged_out(self):
        res = self.app.post_json_api(self.private_project_url,
                                     self.fork_data,
                                     expect_errors=True)
        assert_equal(res.status_code, 401)
        assert_equal(res.json['errors'][0]['detail'],
                     'Authentication credentials were not provided.')

    def test_cannot_fork_private_node_logged_in_non_contributor(self):
        res = self.app.post_json_api(self.private_project_url,
                                     self.fork_data,
                                     auth=self.user_two.auth,
                                     expect_errors=True)
        assert_equal(res.status_code, 403)
        assert_equal(res.json['errors'][0]['detail'],
                     'You do not have permission to perform this action.')

    def test_can_fork_private_node_logged_in_contributor(self):
        res = self.app.post_json_api(
            self.private_project_url +
            '?embed=children&embed=node_links&embed=logs&embed=contributors&embed=forked_from',
            self.fork_data,
            auth=self.user.auth)
        assert_equal(res.status_code, 201)

        data = res.json['data']
        assert_equal(data['attributes']['title'],
                     'Fork of ' + self.private_project.title)

        fork_contributors = data['embeds']['contributors']['data'][0][
            'embeds']['users']['data']
        assert_equal(fork_contributors['attributes']['family_name'],
                     self.user.family_name)
        assert_equal(fork_contributors['id'], self.user._id)

        forked_from = data['embeds']['forked_from']['data']
        assert_equal(forked_from['id'], self.private_project._id)

    def test_fork_private_components_no_access(self):
        url = self.public_project_url + '?embed=children'
        private_component = NodeFactory(parent=self.public_project,
                                        creator=self.user_two,
                                        is_public=False)
        res = self.app.post_json_api(url,
                                     self.fork_data,
                                     auth=self.user_three.auth)
        assert_equal(res.status_code, 201)
        # Private components that you do not have access to are not forked
        assert_equal(
            res.json['data']['embeds']['children']['links']['meta']['total'],
            0)

    def test_fork_components_you_can_access(self):
        url = self.private_project_url + '?embed=children'
        new_component = NodeFactory(parent=self.private_project,
                                    creator=self.user)
        res = self.app.post_json_api(url, self.fork_data, auth=self.user.auth)
        assert_equal(res.status_code, 201)
        assert_equal(
            res.json['data']['embeds']['children']['links']['meta']['total'],
            1)
        assert_equal(res.json['data']['embeds']['children']['data'][0]['id'],
                     new_component.forks[0]._id)
        assert_equal(
            res.json['data']['embeds']['children']['data'][0]['attributes']
            ['title'], new_component.title)

    def test_fork_private_node_links(self):
        private_pointer = ProjectFactory(creator=self.user_two)
        actual_pointer = self.private_project.add_pointer(private_pointer,
                                                          auth=Auth(
                                                              self.user_two),
                                                          save=True)

        url = self.private_project_url + '?embed=node_links'

        # Node link is forked, but shows up as a private node link
        res = self.app.post_json_api(url, self.fork_data, auth=self.user.auth)
        assert_equal(res.status_code, 201)

        assert_equal(
            res.json['data']['embeds']['node_links']['data'][0]['embeds']
            ['target_node']['errors'][0]['detail'],
            'You do not have permission to perform this action.')
        assert_equal(
            res.json['data']['embeds']['node_links']['links']['meta']['total'],
            1)

        self.private_project.rm_pointer(actual_pointer,
                                        auth=Auth(self.user_two))

    def test_fork_node_links_you_can_access(self):
        pointer = ProjectFactory(creator=self.user)
        self.private_project.add_pointer(pointer,
                                         auth=Auth(self.user_two),
                                         save=True)

        url = self.private_project_url + '?embed=node_links'

        res = self.app.post_json_api(url, self.fork_data, auth=self.user.auth)
        assert_equal(res.status_code, 201)

        assert_equal(
            res.json['data']['embeds']['node_links']['data'][0]['embeds']
            ['target_node']['data']['id'], pointer._id)
        assert_equal(
            res.json['data']['embeds']['node_links']['links']['meta']['total'],
            1)

    def test_can_fork_registration(self):
        registration = RegistrationFactory(project=self.private_project,
                                           user=self.user)

        url = '/{}registrations/{}/forks/'.format(API_BASE, registration._id)
        res = self.app.post_json_api(url, self.fork_data, auth=self.user.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['id'], registration.forks[0]._id)
        assert_equal(res.json['data']['attributes']['title'],
                     'Fork of ' + registration.title)

    def test_read_only_contributor_can_fork_private_registration(self):
        read_only_user = AuthUserFactory()

        self.private_project.add_contributor(read_only_user,
                                             permissions=[permissions.READ],
                                             save=True)
        res = self.app.post_json_api(self.private_project_url,
                                     self.fork_data,
                                     auth=read_only_user.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['id'], self.private_project.forks[0]._id)
        assert_equal(res.json['data']['attributes']['title'],
                     'Fork of ' + self.private_project.title)
class TestNodeForkCreate(ApiTestCase):

    def setUp(self):
        super(TestNodeForkCreate, self).setUp()
        self.user = AuthUserFactory()
        self.user_two = AuthUserFactory()
        self.user_three = AuthUserFactory()
        self.private_project = ProjectFactory(creator=self.user)

        self.fork_data = {
            'data': {
                'type': 'nodes'
            }
        }

        self.fork_data_with_title = {
            'data': {
                'type': 'nodes',
                'attributes':
                    {'title': 'My Forked Project'}
            }
        }

        self.private_project_url = '/{}nodes/{}/forks/'.format(API_BASE, self.private_project._id)

        self.public_project = ProjectFactory(is_public=True, creator=self.user)
        self.public_project_url = '/{}nodes/{}/forks/'.format(API_BASE, self.public_project._id)

    def tearDown(self):
        super(TestNodeForkCreate, self).tearDown()
        Node.remove()

    def test_create_fork_from_public_project_with_new_title(self):
        res = self.app.post_json_api(self.public_project_url, self.fork_data_with_title, auth=self.user.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['id'], self.public_project.forks[0]._id)
        assert_equal(res.json['data']['attributes']['title'], self.fork_data_with_title['data']['attributes']['title'])

    def test_create_fork_from_private_project_with_new_title(self):
        res = self.app.post_json_api(self.private_project_url, self.fork_data_with_title, auth=self.user.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['id'], self.private_project.forks[0]._id)
        assert_equal(res.json['data']['attributes']['title'], self.fork_data_with_title['data']['attributes']['title'])

    def test_can_fork_public_node_logged_in(self):
        res = self.app.post_json_api(self.public_project_url, self.fork_data, auth=self.user_two.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['id'], self.public_project.forks[0]._id)
        assert_equal(res.json['data']['attributes']['title'], 'Fork of ' + self.public_project.title)

    def test_cannot_fork_public_node_logged_out(self):
        res = self.app.post_json_api(self.public_project_url, self.fork_data, expect_errors=True)
        assert_equal(res.status_code, 401)
        assert_equal(res.json['errors'][0]['detail'], 'Authentication credentials were not provided.')

    def test_can_fork_public_node_logged_in_contributor(self):
        res = self.app.post_json_api(self.public_project_url, self.fork_data, auth=self.user.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['id'], self.public_project.forks[0]._id)
        assert_equal(res.json['data']['attributes']['title'], 'Fork of ' + self.public_project.title)

    def test_cannot_fork_private_node_logged_out(self):
        res = self.app.post_json_api(self.private_project_url, self.fork_data, expect_errors=True)
        assert_equal(res.status_code, 401)
        assert_equal(res.json['errors'][0]['detail'], 'Authentication credentials were not provided.')

    def test_cannot_fork_private_node_logged_in_non_contributor(self):
        res = self.app.post_json_api(self.private_project_url, self.fork_data, auth=self.user_two.auth, expect_errors=True)
        assert_equal(res.status_code, 403)
        assert_equal(res.json['errors'][0]['detail'], 'You do not have permission to perform this action.')

    def test_can_fork_private_node_logged_in_contributor(self):
        res = self.app.post_json_api(self.private_project_url + '?embed=children&embed=node_links&embed=logs&embed=contributors&embed=forked_from', self.fork_data, auth=self.user.auth)
        assert_equal(res.status_code, 201)

        data = res.json['data']
        assert_equal(data['attributes']['title'], 'Fork of ' + self.private_project.title)

        fork_contributors = data['embeds']['contributors']['data'][0]['embeds']['users']['data']
        assert_equal(fork_contributors['attributes']['family_name'], self.user.family_name)
        assert_equal(fork_contributors['id'], self.user._id)

        forked_from = data['embeds']['forked_from']['data']
        assert_equal(forked_from['id'], self.private_project._id)

    def test_fork_private_components_no_access(self):
        url = self.public_project_url + '?embed=children'
        private_component = NodeFactory(parent=self.public_project, creator=self.user_two, is_public=False)
        res = self.app.post_json_api(url, self.fork_data, auth=self.user_three.auth)
        assert_equal(res.status_code, 201)
        # Private components that you do not have access to are not forked
        assert_equal(res.json['data']['embeds']['children']['links']['meta']['total'], 0)

    def test_fork_components_you_can_access(self):
        url = self.private_project_url + '?embed=children'
        new_component = NodeFactory(parent=self.private_project, creator=self.user)
        res = self.app.post_json_api(url, self.fork_data, auth=self.user.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['embeds']['children']['links']['meta']['total'], 1)
        assert_equal(res.json['data']['embeds']['children']['data'][0]['id'], new_component.forks[0]._id)

    def test_fork_private_node_links(self):
        private_pointer = ProjectFactory(creator=self.user_two)
        actual_pointer = self.private_project.add_pointer(private_pointer, auth=Auth(self.user_two), save=True)

        url = self.private_project_url + '?embed=node_links'

        # Node link is forked, but shows up as a private node link
        res = self.app.post_json_api(url, self.fork_data, auth=self.user.auth)
        assert_equal(res.status_code, 201)

        assert_equal(res.json['data']['embeds']['node_links']['data'][0]['embeds']['target_node']['errors'][0]['detail'],
                     'You do not have permission to perform this action.')
        assert_equal(res.json['data']['embeds']['node_links']['links']['meta']['total'], 1)

        self.private_project.rm_pointer(actual_pointer, auth=Auth(self.user_two))

    def test_fork_node_links_you_can_access(self):
        pointer = ProjectFactory(creator=self.user)
        self.private_project.add_pointer(pointer, auth=Auth(self.user_two), save=True)

        url = self.private_project_url + '?embed=node_links'

        res = self.app.post_json_api(url, self.fork_data, auth=self.user.auth)
        assert_equal(res.status_code, 201)

        assert_equal(res.json['data']['embeds']['node_links']['data'][0]['embeds']['target_node']['data']['id'], pointer._id)
        assert_equal(res.json['data']['embeds']['node_links']['links']['meta']['total'], 1)

    def test_can_fork_registration(self):
        registration = RegistrationFactory(project=self.private_project, user=self.user)

        url = '/{}registrations/{}/forks/'.format(API_BASE, registration._id)
        res = self.app.post_json_api(url, self.fork_data, auth=self.user.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['id'], registration.forks[0]._id)
        assert_equal(res.json['data']['attributes']['title'], 'Fork of ' + registration.title)

    def test_read_only_contributor_can_fork_private_registration(self):
        read_only_user = AuthUserFactory()

        self.private_project.add_contributor(read_only_user, permissions=[permissions.READ], save=True)
        res = self.app.post_json_api(self.private_project_url, self.fork_data, auth=read_only_user.auth)
        assert_equal(res.status_code, 201)
        assert_equal(res.json['data']['id'], self.private_project.forks[0]._id)
        assert_equal(res.json['data']['attributes']['title'], 'Fork of ' + self.private_project.title)