def setUp(self): super(TestNodeForksList, self).setUp() self.user = AuthUserFactory() self.private_project = ProjectFactory() self.private_project.add_contributor( self.user, permissions=[permissions.READ, permissions.WRITE]) self.private_project.save() self.component = NodeFactory(parent=self.private_project, creator=self.user) self.pointer = ProjectFactory(creator=self.user) self.private_project.add_pointer(self.pointer, auth=Auth(self.user), save=True) self.private_fork = ForkFactory(project=self.private_project, user=self.user) 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.save() self.public_component = NodeFactory(parent=self.public_project, creator=self.user, is_public=True) self.public_project_url = '/{}nodes/{}/forks/'.format( API_BASE, self.public_project._id) self.public_fork = ForkFactory(project=self.public_project, user=self.user) self.user_two = AuthUserFactory()
def setUp(self): super(TestRegistrationForksList, self).setUp() self.user = AuthUserFactory() self.private_project = ProjectFactory(creator=self.user) self.private_project.save() self.component = NodeFactory(parent=self.private_project, creator=self.user) self.pointer = ProjectFactory(creator=self.user) self.private_project.add_pointer(self.pointer, auth=Auth(self.user), save=True) self.private_registration = RegistrationFactory( project=self.private_project, creator=self.user) self.private_fork = ForkFactory(project=self.private_registration, user=self.user) self.private_registration_url = '/{}registrations/{}/forks/'.format( API_BASE, self.private_registration._id) self.public_project = ProjectFactory(is_public=True, creator=self.user) self.public_project.save() self.public_component = NodeFactory(parent=self.public_project, creator=self.user, is_public=True) self.public_registration = RegistrationFactory( project=self.public_project, creator=self.user, is_public=True) self.public_registration_url = '/{}registrations/{}/forks/'.format( API_BASE, self.public_registration._id) self.public_fork = ForkFactory(project=self.public_registration, user=self.user) self.user_two = AuthUserFactory()
class TestRegistrationForksList(ApiTestCase): def setUp(self): super(TestRegistrationForksList, self).setUp() self.user = AuthUserFactory() self.private_project = ProjectFactory(creator=self.user) self.private_project.save() self.component = NodeFactory(parent=self.private_project, creator=self.user) self.pointer = ProjectFactory(creator=self.user) self.private_project.add_pointer(self.pointer, auth=Auth(self.user), save=True) self.private_registration = RegistrationFactory( project=self.private_project, creator=self.user) self.private_fork = ForkFactory(project=self.private_registration, user=self.user) self.private_registration_url = '/{}registrations/{}/forks/'.format( API_BASE, self.private_registration._id) self.public_project = ProjectFactory(is_public=True, creator=self.user) self.public_project.save() self.public_component = NodeFactory(parent=self.public_project, creator=self.user, is_public=True) self.public_registration = RegistrationFactory( project=self.public_project, creator=self.user, is_public=True) self.public_registration_url = '/{}registrations/{}/forks/'.format( API_BASE, self.public_registration._id) self.public_fork = ForkFactory(project=self.public_registration, user=self.user) self.user_two = AuthUserFactory() def test_can_access_public_registration_forks_list_when_unauthenticated( self): res = self.app.get(self.public_registration_url) assert_equal(len(res.json['data']), 0) # Fork defaults to private assert_equal(self.public_fork.is_public, False) self.public_fork.is_public = True self.public_fork.save() res = self.app.get(self.public_registration_url) assert_equal(res.status_code, 200) assert_equal(len(res.json['data']), 1) assert_equal(self.public_fork.is_public, True) data = res.json['data'][0] assert_equal(data['attributes']['title'], 'Fork of ' + self.public_registration.title) assert_equal(data['id'], self.public_fork._id) assert_equal(data['attributes']['registration'], False) assert_equal(data['attributes']['fork'], True) def test_can_access_public_registration_forks_list_authenticated_contributor( self): res = self.app.get(self.public_registration_url, auth=self.user.auth) assert_equal(res.status_code, 200) assert_equal(self.public_fork.is_public, False) assert_equal(len(res.json['data']), 1) data = res.json['data'][0] assert_equal(data['attributes']['title'], 'Fork of ' + self.public_project.title) assert_equal(data['id'], self.public_fork._id) assert_equal(data['attributes']['registration'], False) assert_equal(data['attributes']['fork'], True) def test_can_access_public_registration_forks_list_authenticated_non_contributor( self): res = self.app.get(self.public_registration_url, auth=self.user_two.auth) assert_equal(res.status_code, 200) assert_equal(len(res.json['data']), 0) # Fork defaults to private assert_equal(self.public_fork.is_public, False) self.public_fork.is_public = True self.public_fork.save() res = self.app.get(self.public_registration_url) assert_equal(len(res.json['data']), 1) assert_equal(self.public_fork.is_public, True) data = res.json['data'][0] assert_equal(data['attributes']['title'], 'Fork of ' + self.public_project.title) assert_equal(data['id'], self.public_fork._id) assert_equal(data['attributes']['registration'], False) assert_equal(data['attributes']['fork'], True) def test_cannot_access_private_registration_forks_list_unauthenticated( self): res = self.app.get(self.private_registration_url, expect_errors=True) assert_equal(res.status_code, 401) assert_equal(res.json['errors'][0]['detail'], 'Authentication credentials were not provided.') def test_authenticated_contributor_can_access_private_registration_forks_list( self): res = self.app.get( self.private_registration_url + '?embed=children&embed=node_links&embed=logs&embed=contributors&embed=forked_from', auth=self.user.auth) assert_equal(res.status_code, 200) assert_equal(len(res.json['data']), 1) data = res.json['data'][0] assert_equal(data['attributes']['title'], 'Fork of ' + self.private_project.title) assert_equal(data['id'], self.private_fork._id) 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_children = data['embeds']['children']['data'][0] assert_equal(forked_children['id'], self.private_registration.forks.first().nodes[0]._id) assert_equal(forked_children['attributes']['title'], self.component.title) forked_node_links = data['embeds']['node_links']['data'][0]['embeds'][ 'target_node']['data'] assert_equal(forked_node_links['id'], self.pointer._id) assert_equal(forked_node_links['attributes']['title'], self.pointer.title) assert_equal(data['attributes']['registration'], False) assert_equal(data['attributes']['fork'], True) expected_logs = list( self.private_registration.logs.values_list('action', flat=True)) expected_logs.append( self.private_registration.nodes[0].logs.latest().action) expected_logs.append('node_forked') expected_logs.append('node_forked') forked_logs = data['embeds']['logs']['data'] assert_equal(set(expected_logs), set(log['attributes']['action'] for log in forked_logs)) assert_equal(len(forked_logs), len(expected_logs)) forked_from = data['embeds']['forked_from']['data'] assert_equal(forked_from['id'], self.private_registration._id) def test_authenticated_non_contributor_cannot_access_private_registration_forks_list( self): res = self.app.get(self.private_registration_url, 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 public_fork(self, user, public_project): return ForkFactory(project=public_project, user=user)
def private_fork(self, user, private_project): return ForkFactory(project=private_project, user=user)
def public_fork(self, user, public_registration): return ForkFactory(project=public_registration, user=user)
def private_fork(self, user, private_registration): return ForkFactory(project=private_registration, user=user)