def test_get_user_emails_whit_errors():
    with pytest.raises(github.GitHubApiError) as e, \
            patch("taiga_contrib_github_auth.connector.requests") as m_requests:
        m_requests.get.return_value = m_response = Mock()
        m_response.status_code = 401
        m_response.json.return_value = {"error": "Invalid credentials"}

        emails = github.get_user_emails(github.HEADERS)
    assert e.value.status_code == 400
    assert e.value.detail["status_code"] == 401
    assert e.value.detail["error"] == "Invalid credentials"
Beispiel #2
0
def test_get_user_emails_whit_errors():
    with pytest.raises(github.GitHubApiError) as e, \
            patch("taiga_contrib_github_auth.connector.requests") as m_requests:
        m_requests.get.return_value = m_response = Mock()
        m_response.status_code = 401
        m_response.json.return_value = {"error": "Invalid credentials"}

        emails = github.get_user_emails(github.HEADERS)
    assert e.value.status_code == 400
    assert e.value.detail["status_code"] == 401
    assert e.value.detail["error"] == "Invalid credentials"
def test_get_user_emails_success():
    with patch("taiga_contrib_github_auth.connector.requests") as m_requests:
        m_requests.get.return_value = m_response = Mock()
        m_response.status_code = 200
        m_response.json.return_value = [{"email": "*****@*****.**", "primary": False},
                                        {"email": "*****@*****.**", "primary": True}]

        emails = github.get_user_emails(github.HEADERS)

        assert len(emails) == 2
        assert emails[0].email == "*****@*****.**"
        assert not emails[0].is_primary
        assert emails[1].email == "*****@*****.**"
        assert emails[1].is_primary
        m_requests.get.assert_called_once_with("https://api.github.com/user/emails",
                                               headers=github.HEADERS)
Beispiel #4
0
def test_get_user_emails_success():
    with patch("taiga_contrib_github_auth.connector.requests") as m_requests:
        m_requests.get.return_value = m_response = Mock()
        m_response.status_code = 200
        m_response.json.return_value = [{
            "email": "*****@*****.**",
            "primary": False
        }, {
            "email": "*****@*****.**",
            "primary": True
        }]

        emails = github.get_user_emails(github.HEADERS)

        assert len(emails) == 2
        assert emails[0].email == "*****@*****.**"
        assert not emails[0].is_primary
        assert emails[1].email == "*****@*****.**"
        assert emails[1].is_primary
        m_requests.get.assert_called_once_with(
            "https://api.github.com/user/emails", headers=github.HEADERS)
Beispiel #5
0
def github_login_func(request):
    logger.debug(
        "Attempting login using taiga_contrib_github_extended_auth plugin....")

    code = request.DATA.get('code', None)
    token = request.DATA.get('token', None)

    auth_info = login(code)

    headers = connector.HEADERS.copy()
    headers["Authorization"] = "token {}".format(auth_info.access_token)

    user_info = connector.get_user_profile(headers=headers)
    username = user_info.username
    logger.debug("username: {0}".format(username))

    organization = getattr(settings, "TAIGA_GITHUB_EXTENDED_AUTH_ORG", None)
    logger.debug("organization: {0}".format(organization))

    if organization and check_org_membership(
            username, organization, headers=headers):
        logger.debug("confirmed membership...")

        emails = connector.get_user_emails(headers=headers)

        primary_email = next(filter(lambda x: x.is_primary, emails))

        user = github_register(username=username,
                               email=primary_email,
                               full_name=user_info.full_name,
                               github_id=user_info.id,
                               bio=user_info.bio,
                               token=token)

        return make_auth_response_data(user)
    else:
        raise exc.PermissionDenied(
            detail=
            "User {0} was not a member of GitHub organization {1} and is not permitted to register for access to this Taiga instance."
            .format(username, organization))
Beispiel #6
0
def github_login_func(request):
    logger.debug("Attempting login using taiga_contrib_github_extended_auth plugin....")

    code = request.DATA.get('code', None)
    token = request.DATA.get('token', None)

    auth_info = login(code)

    headers = connector.HEADERS.copy()
    headers["Authorization"] = "token {}".format(auth_info.access_token)

    user_info = connector.get_user_profile(headers=headers)
    username = user_info.username
    logger.debug("username: {0}".format(username))

    organization = getattr(settings, "TAIGA_GITHUB_EXTENDED_AUTH_ORG",  None)
    logger.debug("organization: {0}".format(organization))

    if organization and check_org_membership(username, organization, headers=headers):
        logger.debug("confirmed membership...")

        emails = connector.get_user_emails(headers=headers)

        primary_email = next(filter(lambda x: x.is_primary, emails)).email

        logger.debug("Primary email is {}".format(primary_email))

        user = github_register(username=username,
                               email=primary_email.lower(),
                               full_name=user_info.full_name,
                               github_id=user_info.id,
                               bio=user_info.bio,
                               token=token)

        return make_auth_response_data(user)
    else:
        raise exc.PermissionDenied(detail="User {0} was not a member of GitHub organization {1} and is not permitted to register for access to this Taiga instance.".format(username, organization))