Exemplo n.º 1
0
 def test_permissions(self, mock_has_auth):
     gitlab_mock = self.gitlab
     mock_has_auth.return_value = True
     connection = gitlab_mock
     with mock.patch('osf.models.node.AbstractNode.is_registration', new_callable=mock.PropertyMock) as mock_is_reg:
         mock_is_reg.return_value = True
         assert_false(check_permissions(self.node_settings, self.consolidated_auth, connection, 'master'))
Exemplo n.º 2
0
def gitlab_hgrid_data(node_settings, auth, **kwargs):

    # Quit if no repo linked
    if not node_settings.complete:
        return

    connection = GitLabClient(external_account=node_settings.external_account)

    # Initialize repo here in the event that it is set in the privacy check
    # below. This potentially saves an API call in _check_permissions, below.
    repo = None

    # Quit if privacy mismatch and not contributor
    node = node_settings.owner
    if node.is_public or node.is_contributor(auth.user):
        try:
            repo = connection.repo(node_settings.repo_id)
        except NotFoundError:
            logger.error('Could not access GitLab repo')
            return None

    try:
        branch, sha, branches = get_refs(node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection)
    except (NotFoundError, GitLabError):
        logger.error('GitLab repo not found')
        return

    if branch is not None:
        ref = ref_to_params(branch, sha)
        can_edit = check_permissions(node_settings, auth, connection, branch, sha, repo=repo)
    else:
        ref = ''
        can_edit = False

    permissions = {
        'edit': can_edit,
        'view': True,
        'private': node_settings.is_private
    }
    urls = {
        'upload': node_settings.owner.api_url + 'gitlab/file/' + ref,
        'fetch': node_settings.owner.api_url + 'gitlab/hgrid/' + ref,
        'branch': node_settings.owner.api_url + 'gitlab/hgrid/root/' + ref,
        'zip': 'https://{0}/{1}/repository/archive.zip?branch={2}'.format(node_settings.external_account.oauth_secret, repo['path_with_namespace'], ref),
        'repo': 'https://{0}/{1}/tree/{2}'.format(node_settings.external_account.oauth_secret, repo['path_with_namespace'], ref)
    }

    branch_names = [each['name'] for each in branches]
    if not branch_names:
        branch_names = [branch]  # if repo un-init-ed then still add default branch to list of branches

    return [rubeus.build_addon_root(
        node_settings,
        repo['path_with_namespace'],
        urls=urls,
        permissions=permissions,
        branches=branch_names,
        private_key=kwargs.get('view_only', None),
        default_branch=repo['default_branch'],
    )]
Exemplo n.º 3
0
def gitlab_hgrid_data(node_settings, auth, **kwargs):

    # Quit if no repo linked
    if not node_settings.complete:
        return

    connection = GitLabClient(external_account=node_settings.external_account)

    # Initialize repo here in the event that it is set in the privacy check
    # below. This potentially saves an API call in _check_permissions, below.
    repo = None

    # Quit if privacy mismatch and not contributor
    node = node_settings.owner
    if node.is_public or node.is_contributor(auth.user):
        try:
            repo = connection.repo(node_settings.repo_id)
        except NotFoundError:
            logger.error('Could not access GitLab repo')
            return None

    try:
        branch, sha, branches = get_refs(node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection)
    except (NotFoundError, GitLabError):
        logger.error('GitLab repo not found')
        return

    if branch is not None:
        ref = ref_to_params(branch, sha)
        can_edit = check_permissions(node_settings, auth, connection, branch, sha, repo=repo)
    else:
        ref = ''
        can_edit = False

    permissions = {
        'edit': can_edit,
        'view': True,
        'private': node_settings.is_private
    }
    urls = {
        'upload': node_settings.owner.api_url + 'gitlab/file/' + ref,
        'fetch': node_settings.owner.api_url + 'gitlab/hgrid/' + ref,
        'branch': node_settings.owner.api_url + 'gitlab/hgrid/root/' + ref,
        'zip': 'https://{0}/{1}/repository/archive.zip?branch={2}'.format(node_settings.external_account.oauth_secret, repo.path_with_namespace, ref),
        'repo': 'https://{0}/{1}/tree/{2}'.format(node_settings.external_account.oauth_secret, repo.path_with_namespace, ref)
    }

    branch_names = [each.name for each in branches]
    if not branch_names:
        branch_names = [branch]  # if repo un-init-ed then still add default branch to list of branches

    return [rubeus.build_addon_root(
        node_settings,
        repo.path_with_namespace,
        urls=urls,
        permissions=permissions,
        branches=branch_names,
        private_key=kwargs.get('view_only', None),
        default_branch=repo.default_branch,
    )]
Exemplo n.º 4
0
 def test_permissions(self, mock_repo, mock_has_auth):
     gitlab_mock = self.gitlab
     mock_has_auth.return_value = True
     connection = gitlab_mock
     mock_repository = mock.Mock(
         **{
             'user': '******',
             'repo': 'mock-repo',
             'permissions': {
                 'project_access': {
                     'access_level': 20,
                     'notification_level': 3
                 }
             },
         })
     mock_repo.attributes.return_value = mock_repository
     with mock.patch('osf.models.node.AbstractNode.is_registration',
                     new_callable=mock.PropertyMock) as mock_is_reg:
         mock_is_reg.return_value = True
         assert_false(
             check_permissions(self.node_settings,
                               self.consolidated_auth,
                               connection,
                               'master',
                               repo=mock_repository))
Exemplo n.º 5
0
 def test_permissions_not_head(self, mock_repo, mock_has_auth):
     gitlab_mock = self.gitlab
     mock_has_auth.return_value = True
     connection = gitlab_mock
     mock_branch = mock.Mock(**{'commit': {'id': '67890'}})
     mock_repository = mock.Mock(
         **{
             'user': '******',
             'repo': 'mock-repo',
             'permissions': {
                 'project_access': {
                     'access_level': 20,
                     'notification_level': 3
                 }
             },
         })
     mock_repo.attributes.return_value = mock_repository
     connection.branches.return_value = mock_branch
     sha = '12345'
     assert_false(
         check_permissions(self.node_settings,
                           self.consolidated_auth,
                           connection,
                           mock_branch,
                           sha=sha,
                           repo=mock_repository))
Exemplo n.º 6
0
    def test_permissions_no_auth(self, mock_repo):
        gitlab_mock = self.gitlab
        # project is set to private right now
        mock_repository = mock.Mock(
            **{
                'user': '******',
                'repo': 'mock-repo',
                'permissions': {
                    'project_access': {
                        'access_level': 20,
                        'notification_level': 3
                    }
                },
            })
        mock_repo.attributes.return_value = mock_repository

        connection = gitlab_mock
        non_authenticated_user = UserFactory()
        non_authenticated_auth = Auth(user=non_authenticated_user)
        branch = 'master'
        assert_false(
            check_permissions(self.node_settings,
                              non_authenticated_auth,
                              connection,
                              branch,
                              repo=mock_repository))
Exemplo n.º 7
0
 def test_permissions_no_auth(self):
     gitlab_mock = self.gitlab
     # project is set to private right now
     connection = gitlab_mock
     non_authenticated_user = UserFactory()
     non_authenticated_auth = Auth(user=non_authenticated_user)
     branch = 'master'
     assert_false(check_permissions(self.node_settings, non_authenticated_auth, connection, branch))
Exemplo n.º 8
0
 def test_permissions(self, mock_has_auth):
     gitlab_mock = self.gitlab
     mock_has_auth.return_value = True
     connection = gitlab_mock
     with mock.patch('osf.models.node.AbstractNode.is_registration',
                     new_callable=mock.PropertyMock) as mock_is_reg:
         mock_is_reg.return_value = True
         assert_false(
             check_permissions(self.node_settings, self.consolidated_auth,
                               connection, 'master'))
Exemplo n.º 9
0
 def test_permissions_no_auth(self):
     gitlab_mock = self.gitlab
     # project is set to private right now
     connection = gitlab_mock
     non_authenticated_user = UserFactory()
     non_authenticated_auth = Auth(user=non_authenticated_user)
     branch = 'master'
     assert_false(
         check_permissions(self.node_settings, non_authenticated_auth,
                           connection, branch))
Exemplo n.º 10
0
 def test_permissions_not_head(self, mock_repo, mock_has_auth):
     gitlab_mock = self.gitlab
     mock_has_auth.return_value = True
     connection = gitlab_mock
     mock_branch = mock.Mock(**{
         'commit': {'id': '67890'}
     })
     connection.branches.return_value = mock_branch
     sha = '12345'
     assert_false(check_permissions(self.node_settings, self.consolidated_auth, connection, mock_branch, sha=sha))
Exemplo n.º 11
0
 def test_permissions_not_head(self, mock_repo, mock_has_auth):
     gitlab_mock = self.gitlab
     mock_has_auth.return_value = True
     connection = gitlab_mock
     mock_branch = mock.Mock(**{'commit': {'id': '67890'}})
     connection.branches.return_value = mock_branch
     sha = '12345'
     assert_false(
         check_permissions(self.node_settings,
                           self.consolidated_auth,
                           connection,
                           mock_branch,
                           sha=sha))
Exemplo n.º 12
0
 def test_permissions_no_access(self, mock_repo, mock_has_auth):
     gitlab_mock = self.gitlab
     mock_has_auth.return_value = True
     connection = gitlab_mock
     branch = 'master'
     mock_repository = mock.Mock(**{
         'user': '******',
         'repo': 'mock-repo',
         'permissions': {
             'project_access': {'access_level': 20, 'notification_level': 3}
         },
     })
     mock_repo.attributes.return_value = mock_repository
     assert_false(check_permissions(self.node_settings, self.consolidated_auth, connection, branch, repo=mock_repository))
Exemplo n.º 13
0
 def test_permissions_no_access(self, mock_repo, mock_has_auth):
     gitlab_mock = self.gitlab
     mock_has_auth.return_value = True
     connection = gitlab_mock
     branch = 'master'
     mock_repository = {
         'user': '******',
         'repo': 'mock-repo',
         'permissions': {
             'project_access': {
                 'access_level': 20,
                 'notification_level': 3
             }
         },
     }
     mock_repo.return_value = mock_repository
     assert_false(
         check_permissions(self.node_settings,
                           self.consolidated_auth,
                           connection,
                           branch,
                           repo=mock_repository))