Ejemplo n.º 1
0
def test_get_invitations(client, jwt, session):  # pylint:disable=unused-argument
    """Assert that a list of invitations for an org can be retrieved."""
    headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.edit_role)
    rv = client.post('/api/v1/users',
                     headers=headers,
                     content_type='application/json')
    rv = client.post('/api/v1/orgs',
                     data=json.dumps(TestOrgInfo.org1),
                     headers=headers,
                     content_type='application/json')
    dictionary = json.loads(rv.data)
    org_id = dictionary['id']

    rv = client.post('/api/v1/invitations',
                     data=json.dumps(
                         factory_invitation(org_id, '*****@*****.**')),
                     headers=headers,
                     content_type='application/json')

    rv = client.post('/api/v1/invitations',
                     data=json.dumps(
                         factory_invitation(org_id, '*****@*****.**')),
                     headers=headers,
                     content_type='application/json')

    rv = client.get('/api/v1/orgs/{}/invitations'.format(org_id),
                    headers=headers,
                    content_type='application/json')

    assert rv.status_code == http_status.HTTP_200_OK
    dictionary = json.loads(rv.data)
    assert dictionary['invitations']
    assert len(dictionary['invitations']) == 2
    assert dictionary['invitations'][0]['recipientEmail'] == '*****@*****.**'
    assert dictionary['invitations'][1]['recipientEmail'] == '*****@*****.**'
Ejemplo n.º 2
0
def test_validate_token(client, jwt, session, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that a token is valid."""
    headers = factory_auth_header(jwt=jwt,
                                  claims=TestJwtClaims.public_user_role)
    client.post('/api/v1/users',
                headers=headers,
                content_type='application/json')
    rv = client.post('/api/v1/orgs',
                     data=json.dumps(TestOrgInfo.org1),
                     headers=headers,
                     content_type='application/json')
    dictionary = json.loads(rv.data)
    org_id = dictionary['id']
    rv = client.post('/api/v1/invitations',
                     data=json.dumps(factory_invitation(org_id=org_id)),
                     headers=headers,
                     content_type='application/json')
    invitation_dictionary = json.loads(rv.data)
    invitation_id = invitation_dictionary['id']
    invitation_id_token = InvitationService.generate_confirmation_token(
        invitation_id)
    rv = client.get(
        '/api/v1/invitations/tokens/{}'.format(invitation_id_token),
        headers=headers,
        content_type='application/json')
    assert rv.status_code == http_status.HTTP_200_OK
Ejemplo n.º 3
0
def test_accept_invitation(session, auth_mock, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Accept the invitation and add membership from the invitation to the org."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        with patch.object(auth, 'check_auth', return_value=True):
            with patch.object(InvitationService,
                              'notify_admin',
                              return_value=None):
                user_with_token = TestUserInfo.user_test
                user_with_token[
                    'keycloak_guid'] = TestJwtClaims.public_user_role['sub']
                user = factory_user_model(user_with_token)
                patch_token_info({'sub': user.keycloak_guid}, monkeypatch)

                org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
                org_dictionary = org.as_dict()
                invitation_info = factory_invitation(org_dictionary['id'])
                user_with_token_invitee = TestUserInfo.user1
                user_with_token_invitee[
                    'keycloak_guid'] = TestJwtClaims.edit_role_2['sub']
                user_invitee = factory_user_model(user_with_token_invitee)
                new_invitation = InvitationService.create_invitation(
                    invitation_info, User(user_invitee), '')
                new_invitation_dict = new_invitation.as_dict()
                InvitationService.accept_invitation(new_invitation_dict['id'],
                                                    User(user_invitee), '')
                patch_token_info(TestJwtClaims.public_user_role, monkeypatch)
                members = MembershipService.get_members_for_org(
                    org_dictionary['id'], 'PENDING_APPROVAL')
                assert members
                assert len(members) == 1
Ejemplo n.º 4
0
def test_delete_invitation(client, jwt, session, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that an invitation can be deleted."""
    headers = factory_auth_header(jwt=jwt,
                                  claims=TestJwtClaims.public_user_role)
    rv = client.post('/api/v1/users',
                     headers=headers,
                     content_type='application/json')
    rv = client.post('/api/v1/orgs',
                     data=json.dumps(TestOrgInfo.org1),
                     headers=headers,
                     content_type='application/json')
    dictionary = json.loads(rv.data)
    org_id = dictionary['id']
    rv = client.post('/api/v1/invitations',
                     data=json.dumps(factory_invitation(org_id=org_id)),
                     headers=headers,
                     content_type='application/json')
    invitation_dictionary = json.loads(rv.data)
    invitation_id = invitation_dictionary['id']
    rv = client.delete('/api/v1/invitations/{}'.format(invitation_id),
                       headers=headers,
                       content_type='application/json')
    assert rv.status_code == http_status.HTTP_200_OK

    rv = client.get('/api/v1/invitations/{}'.format(invitation_id),
                    headers=headers,
                    content_type='application/json')
    assert rv.status_code == http_status.HTTP_404_NOT_FOUND
    dictionary = json.loads(rv.data)
    assert dictionary[
        'message'] == 'The requested invitation could not be found.'
Ejemplo n.º 5
0
def test_create_invitation(session, auth_mock, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Assert that an Invitation can be created."""
    with patch.object(InvitationService, 'send_invitation',
                      return_value=None) as mock_notify:
        user = factory_user_model(TestUserInfo.user_test)
        patch_token_info({'sub': user.keycloak_guid}, monkeypatch)
        org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
        org_dictionary = org.as_dict()
        invitation_info = factory_invitation(org_dictionary['id'])

        with patch.object(ActivityLogPublisher,
                          'publish_activity',
                          return_value=None) as mock_alp:
            invitation = InvitationService.create_invitation(
                invitation_info, User(user), '')
            mock_alp.assert_called_with(
                Activity(action=ActivityAction.INVITE_TEAM_MEMBER.value,
                         org_id=ANY,
                         name=invitation_info['recipientEmail'],
                         id=ANY,
                         value='USER'))

        invitation_dictionary = invitation.as_dict()
        assert invitation_dictionary['recipient_email'] == invitation_info[
            'recipientEmail']
        assert invitation_dictionary['id']
        mock_notify.assert_called()
Ejemplo n.º 6
0
def test_update_invitation(client, jwt, session, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that an invitation can be updated."""
    headers = factory_auth_header(jwt=jwt,
                                  claims=TestJwtClaims.public_user_role)
    rv = client.post('/api/v1/users',
                     headers=headers,
                     content_type='application/json')
    rv = client.post('/api/v1/orgs',
                     data=json.dumps(TestOrgInfo.org1),
                     headers=headers,
                     content_type='application/json')
    dictionary = json.loads(rv.data)
    org_id = dictionary['id']
    rv = client.post('/api/v1/invitations',
                     data=json.dumps(factory_invitation(org_id=org_id)),
                     headers=headers,
                     content_type='application/json')
    invitation_dictionary = json.loads(rv.data)
    invitation_id = invitation_dictionary['id']
    updated_invitation = {}
    rv = client.patch('/api/v1/invitations/{}'.format(invitation_id),
                      data=json.dumps(updated_invitation),
                      headers=headers,
                      content_type='application/json')
    assert rv.status_code == http_status.HTTP_200_OK
    assert schema_utils.validate(rv.json, 'invitation_response')[0]
    dictionary = json.loads(rv.data)
    assert dictionary['status'] == 'PENDING'
Ejemplo n.º 7
0
def test_accept_gov_account_invitation(client, jwt, session):  # pylint:disable=unused-argument
    """Assert that an invitation can be accepted."""
    headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.gov_account_holder_user)
    client.post('/api/v1/users', headers=headers, content_type='application/json')
    rv = client.post('/api/v1/orgs', data=json.dumps(TestOrgInfo.org1),
                     headers=headers, content_type='application/json')
    dictionary = json.loads(rv.data)
    org_id = dictionary['id']
    rv = client.post('/api/v1/invitations', data=json.dumps(factory_invitation(org_id=org_id)),
                     headers=headers, content_type='application/json')
    invitation_dictionary = json.loads(rv.data)
    invitation_id = invitation_dictionary['id']
    invitation_id_token = InvitationService.generate_confirmation_token(invitation_id)

    request = KeycloakScenario.create_user_request()
    KEYCLOAK_SERVICE.add_user(request, return_if_exists=True)
    user = KEYCLOAK_SERVICE.get_user_by_username(request.user_name)
    user_id = user.id
    headers_invitee = factory_auth_header(jwt=jwt, claims=TestJwtClaims.get_test_user(user_id, source='IDIR'))
    client.post('/api/v1/users', headers=headers_invitee, content_type='application/json')
    rv = client.put('/api/v1/invitations/tokens/{}'.format(invitation_id_token), headers=headers_invitee,
                    content_type='application/json')
    assert rv.status_code == http_status.HTTP_200_OK

    rv = client.get('/api/v1/orgs/{}/members?status=PENDING_APPROVAL'.format(org_id),
                    headers=headers,
                    content_type='application/json')
    dictionary = json.loads(rv.data)
    assert len(dictionary['members']) == 1
    # Assert that the user got added to the keycloak groups
    user_groups = KEYCLOAK_SERVICE.get_user_groups(user_id=user_id)
    groups = []
    for group in user_groups:
        groups.append(group.get('name'))
    assert GROUP_GOV_ACCOUNT_USERS in groups
Ejemplo n.º 8
0
def test_accept_invitation(client, jwt, session):  # pylint:disable=unused-argument
    """Assert that an invitation can be accepted."""
    headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.edit_role)
    client.post('/api/v1/users',
                headers=headers,
                content_type='application/json')
    rv = client.post('/api/v1/orgs',
                     data=json.dumps(TestOrgInfo.org1),
                     headers=headers,
                     content_type='application/json')
    dictionary = json.loads(rv.data)
    org_id = dictionary['id']
    rv = client.post('/api/v1/invitations',
                     data=json.dumps(factory_invitation(org_id=org_id)),
                     headers=headers,
                     content_type='application/json')
    invitation_dictionary = json.loads(rv.data)
    invitation_id = invitation_dictionary['id']
    invitation_id_token = InvitationService.generate_confirmation_token(
        invitation_id)
    rv = client.put(
        '/api/v1/invitations/tokens/{}'.format(invitation_id_token),
        headers=headers,
        content_type='application/json')
    assert rv.status_code == http_status.HTTP_200_OK
    rv = client.get(
        '/api/v1/orgs/{}/members?status=PENDING_APPROVAL'.format(org_id),
        headers=headers,
        content_type='application/json')
    dictionary = json.loads(rv.data)
    assert len(dictionary['members']) == 1
Ejemplo n.º 9
0
def test_accept_invitation_for_govm(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Accept the invitation and add membership from the invitation to the org."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        with patch.object(auth, 'check_auth', return_value=True):
            with patch.object(InvitationService,
                              'notify_admin',
                              return_value=None):
                user_with_token = TestUserInfo.user_staff_admin
                user_with_token[
                    'keycloak_guid'] = TestJwtClaims.public_user_role['sub']
                user = factory_user_model(user_with_token)
                org = OrgService.create_org(
                    TestOrgInfo.org_govm,
                    user_id=user.id,
                    token_info=TestJwtClaims.staff_admin_role)
                org_dictionary = org.as_dict()
                invitation_info = factory_invitation(org_dictionary['id'])
                user_with_token_invitee = TestUserInfo.user1
                user_with_token_invitee[
                    'keycloak_guid'] = TestJwtClaims.edit_role_2['sub']
                user_invitee = factory_user_model(user_with_token_invitee)
                new_invitation = InvitationService.create_invitation(
                    invitation_info, User(user_invitee), {}, '')
                new_invitation_dict = new_invitation.as_dict()
                InvitationService.accept_invitation(new_invitation_dict['id'],
                                                    User(user_invitee), '')
                members = MembershipService.get_members_for_org(
                    org_dictionary['id'],
                    'ACTIVE',
                    token_info=TestJwtClaims.staff_admin_role)
                assert members
                assert len(members) == 1, 'user gets active membership'
Ejemplo n.º 10
0
def test_accept_invitation_exceptions(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Accept the invitation and add membership from the invitation to the org."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        with patch.object(auth, 'check_auth', return_value=True):
            with patch.object(InvitationService, 'notify_admin', return_value=None):
                user = factory_user_model(TestUserInfo.user_test)
                org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
                org_dictionary = org.as_dict()
                invitation_info = factory_invitation(org_dictionary['id'])

                user_invitee = factory_user_model(TestUserInfo.user1)

                with pytest.raises(BusinessException) as exception:
                    InvitationService.accept_invitation(None, User(user_invitee), '')

                assert exception.value.code == Error.DATA_NOT_FOUND.name

                new_invitation = InvitationService.create_invitation(invitation_info, User(user_invitee), {}, '')
                new_invitation_dict = new_invitation.as_dict()
                InvitationService.accept_invitation(new_invitation_dict['id'], User(user_invitee), '')

                with pytest.raises(BusinessException) as exception:
                    InvitationService.accept_invitation(new_invitation_dict['id'], User(user_invitee), '')

                assert exception.value.code == Error.ACTIONED_INVITATION.name

                with pytest.raises(BusinessException) as exception:
                    expired_invitation: InvitationModel = InvitationModel \
                        .find_invitation_by_id(new_invitation_dict['id'])
                    expired_invitation.invitation_status = InvitationStatusModel.get_status_by_code('EXPIRED')
                    expired_invitation.save()
                    InvitationService.accept_invitation(expired_invitation.id, User(user_invitee), '')

                assert exception.value.code == Error.EXPIRED_INVITATION.name
Ejemplo n.º 11
0
def test_add_invitation_invalid(client, jwt, session):  # pylint:disable=unused-argument
    """Assert that POSTing an invalid invitation returns a 400."""
    headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.edit_role)
    rv = client.post('/api/v1/invitations',
                     data=json.dumps(factory_invitation(org_id=None)),
                     headers=headers,
                     content_type='application/json')
    assert rv.status_code == http_status.HTTP_400_BAD_REQUEST
Ejemplo n.º 12
0
def test_accept_public_users_invitation(
        client,
        jwt,
        session,
        org_info,
        role,  # pylint:disable=unused-argument
        claims,
        source,
        exp_status):
    """Assert that an invitation can be accepted."""
    headers = factory_auth_header(jwt=jwt, claims=claims)
    client.post('/api/v1/users',
                headers=headers,
                content_type='application/json')
    rv = client.post('/api/v1/orgs',
                     data=json.dumps(org_info),
                     headers=headers,
                     content_type='application/json')
    dictionary = json.loads(rv.data)
    org_id = dictionary['id']
    rv = client.post('/api/v1/invitations',
                     data=json.dumps(
                         factory_invitation(org_id=org_id,
                                            membership_type=role)),
                     headers=headers,
                     content_type='application/json')
    invitation_dictionary = json.loads(rv.data)
    invitation_id = invitation_dictionary['id']
    invitation_id_token = InvitationService.generate_confirmation_token(
        invitation_id)

    request = KeycloakScenario.create_user_request()
    KEYCLOAK_SERVICE.add_user(request, return_if_exists=True)
    user = KEYCLOAK_SERVICE.get_user_by_username(request.user_name)
    user_id = user.id
    headers_invitee = factory_auth_header(jwt=jwt,
                                          claims=TestJwtClaims.get_test_user(
                                              user_id, source=source))
    client.post('/api/v1/users',
                headers=headers_invitee,
                content_type='application/json')
    rv = client.put(
        '/api/v1/invitations/tokens/{}'.format(invitation_id_token),
        headers=headers_invitee,
        content_type='application/json')
    assert rv.status_code == http_status.HTTP_200_OK

    rv = client.get(f'/api/v1/orgs/{org_id}/members?status={exp_status.name}',
                    headers=headers,
                    content_type='application/json')
    dictionary = json.loads(rv.data)
    assert len(dictionary['members']) == 1
    # Assert that the user got added to the keycloak groups
    user_groups = KEYCLOAK_SERVICE.get_user_groups(user_id=user_id)
    groups = []
    for group in user_groups:
        groups.append(group.get('name'))
    assert GROUP_PUBLIC_USERS in groups
Ejemplo n.º 13
0
def test_as_dict(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that the Invitation is exported correctly as a dictionary."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        user = factory_user_model()
        org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
        org_dictionary = org.as_dict()
        invitation_info = factory_invitation(org_dictionary['id'])
        invitation = InvitationService.create_invitation(invitation_info, User(user), {}, '')
        invitation_dictionary = invitation.as_dict()
        assert invitation_dictionary['recipient_email'] == invitation_info['recipientEmail']
Ejemplo n.º 14
0
def test_update_invitation(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Update the specified invitation with new data."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        user = factory_user_model(TestUserInfo.user_test)
        org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
        org_dictionary = org.as_dict()
        invitation_info = factory_invitation(org_dictionary['id'])
        new_invitation = InvitationService.create_invitation(invitation_info, User(user), {}, '')
        updated_invitation = new_invitation.update_invitation(User(user), {}, '').as_dict()
        assert updated_invitation['status'] == 'PENDING'
Ejemplo n.º 15
0
def test_find_invitation_by_id(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Find an existing invitation with the provided id."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        user = factory_user_model(TestUserInfo.user_test)
        org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
        org_dictionary = org.as_dict()
        invitation_info = factory_invitation(org_dictionary['id'])
        new_invitation = InvitationService.create_invitation(invitation_info, User(user), {}, '').as_dict()
        invitation = InvitationService.find_invitation_by_id(new_invitation['id']).as_dict()
        assert invitation
        assert invitation['recipient_email'] == invitation_info['recipientEmail']
Ejemplo n.º 16
0
def test_validate_token_valid(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Validate the invitation token."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        user = factory_user_model(TestUserInfo.user_test)
        org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
        org_dictionary = org.as_dict()
        invitation_info = factory_invitation(org_dictionary['id'])
        new_invitation = InvitationService.create_invitation(invitation_info, User(user), {}, '').as_dict()
        confirmation_token = InvitationService.generate_confirmation_token(new_invitation['id'])
        invitation_id = InvitationService.validate_token(confirmation_token).as_dict().get('id')
        assert invitation_id == new_invitation['id']
Ejemplo n.º 17
0
def test_delete_invitation(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Delete the specified invitation."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        user = factory_user_model(TestUserInfo.user_test)
        org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
        org_dictionary = org.as_dict()
        invitation_info = factory_invitation(org_dictionary['id'])
        new_invitation = InvitationService.create_invitation(invitation_info, User(user), {}, '').as_dict()
        InvitationService.delete_invitation(new_invitation['id'])
        invitation = InvitationService.find_invitation_by_id(new_invitation['id'])
        assert invitation is None
Ejemplo n.º 18
0
def test_create_invitation(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that an Invitation can be created."""
    with patch.object(InvitationService, 'send_invitation', return_value=None) as mock_notify:
        user = factory_user_model(TestUserInfo.user_test)
        org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
        org_dictionary = org.as_dict()
        invitation_info = factory_invitation(org_dictionary['id'])
        invitation = InvitationService.create_invitation(invitation_info, User(user), {}, '')
        invitation_dictionary = invitation.as_dict()
        assert invitation_dictionary['recipient_email'] == invitation_info['recipientEmail']
        assert invitation_dictionary['id']
        mock_notify.assert_called()
Ejemplo n.º 19
0
def test_add_invitation(client, jwt, session, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that an invitation can be POSTed."""
    headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.edit_role)
    rv = client.post('/api/v1/users', headers=headers, content_type='application/json')
    rv = client.post('/api/v1/orgs', data=json.dumps(TestOrgInfo.org1),
                     headers=headers, content_type='application/json')
    dictionary = json.loads(rv.data)
    org_id = dictionary['id']
    rv = client.post('/api/v1/invitations', data=json.dumps(factory_invitation(org_id=org_id)),
                     headers=headers, content_type='application/json')
    dictionary = json.loads(rv.data)
    assert dictionary.get('token') is not None
    assert rv.status_code == http_status.HTTP_201_CREATED
Ejemplo n.º 20
0
def test_delete_org_failure_members(client, jwt, session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that a member of an org can have their role updated."""
    # Set up: create/login user, create org
    headers_invitee = factory_auth_header(jwt=jwt, claims=TestJwtClaims.public_user_role)
    rv = client.post('/api/v1/users', headers=headers_invitee, content_type='application/json')
    rv = client.post('/api/v1/orgs', data=json.dumps(TestOrgInfo.org1),
                     headers=headers_invitee, content_type='application/json')
    dictionary = json.loads(rv.data)
    org_id = dictionary['id']

    # Invite a user to the org
    rv = client.post('/api/v1/invitations', data=json.dumps(factory_invitation(org_id, '*****@*****.**')),
                     headers=headers_invitee, content_type='application/json')
    dictionary = json.loads(rv.data)
    invitation_id = dictionary['id']
    invitation_id_token = InvitationService.generate_confirmation_token(invitation_id)

    # Create/login as invited user
    headers_invited = factory_auth_header(jwt=jwt, claims=TestJwtClaims.edit_role_2)
    rv = client.post('/api/v1/users', headers=headers_invited, content_type='application/json')

    # Accept invite as invited user
    rv = client.put('/api/v1/invitations/tokens/{}'.format(invitation_id_token),
                    headers=headers_invited, content_type='application/json')

    assert rv.status_code == http_status.HTTP_200_OK
    dictionary = json.loads(rv.data)
    assert dictionary['status'] == 'ACCEPTED'

    # Get pending members for the org as invitee and assert length of 1
    rv = client.get('/api/v1/orgs/{}/members?status=PENDING_APPROVAL'.format(org_id), headers=headers_invitee)
    assert rv.status_code == http_status.HTTP_200_OK
    dictionary = json.loads(rv.data)
    assert dictionary['members']
    assert len(dictionary['members']) == 1

    # Find the pending member
    new_member = dictionary['members'][0]
    assert new_member['membershipTypeCode'] == 'MEMBER'
    member_id = new_member['id']

    # Update the new member
    rv = client.patch('/api/v1/orgs/{}/members/{}'.format(org_id, member_id), headers=headers_invitee,
                      data=json.dumps({'role': 'ADMIN'}), content_type='application/json')
    assert rv.status_code == http_status.HTTP_200_OK
    dictionary = json.loads(rv.data)
    assert dictionary['membershipTypeCode'] == 'ADMIN'

    headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.passcode)
    rv = client.delete('/api/v1/orgs/{}'.format(org_id), headers=headers, content_type='application/json')
    assert rv.status_code == http_status.HTTP_406_NOT_ACCEPTABLE
Ejemplo n.º 21
0
def test_get_invitations(session, auth_mock):  # pylint:disable=unused-argument
    """Assert that invitations for an org can be retrieved."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        user = factory_user_model()
        org = OrgService.create_org(TestOrgInfo.org1, user.id)

        invitation_info = factory_invitation(org.as_dict()['id'])

        invitation = InvitationService.create_invitation(invitation_info, UserService(user), {}, '')

        response = org.get_invitations()
        assert response
        assert len(response['invitations']) == 1
        assert response['invitations'][0]['recipientEmail'] == invitation.as_dict()['recipientEmail']
Ejemplo n.º 22
0
def test_get_invitations_by_id(client, jwt, session, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that an invitation can be retrieved."""
    headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.edit_role)
    rv = client.post('/api/v1/users', headers=headers, content_type='application/json')
    rv = client.post('/api/v1/orgs', data=json.dumps(TestOrgInfo.org1),
                     headers=headers, content_type='application/json')
    dictionary = json.loads(rv.data)
    org_id = dictionary['id']
    rv = client.post('/api/v1/invitations', data=json.dumps(factory_invitation(org_id=org_id)),
                     headers=headers, content_type='application/json')
    invitation_dictionary = json.loads(rv.data)
    invitation_id = invitation_dictionary['id']
    rv = client.get('/api/v1/invitations/{}'.format(invitation_id), headers=headers, content_type='application/json')
    assert rv.status_code == http_status.HTTP_200_OK
Ejemplo n.º 23
0
def test_update_invitation_verify_different_tokens(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Update the specified invitation with new data."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        user = factory_user_model(TestUserInfo.user_test)
        org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
        org_dictionary = org.as_dict()
        invitation_info = factory_invitation(org_dictionary['id'])
        new_invitation = InvitationService.create_invitation(invitation_info, User(user), {}, '')
        old_token = new_invitation.as_dict().get('token')
        with freeze_time(
                lambda: datetime.now() + timedelta(seconds=1)):  # to give time difference..or else token will be same..
            updated_invitation = new_invitation.update_invitation(User(user), {}, '').as_dict()
            new_token = updated_invitation.get('token')
        assert old_token != new_token
        assert updated_invitation['status'] == 'PENDING'
Ejemplo n.º 24
0
def test_validate_token_accepted(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Validate invalid invitation token."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        user = factory_user_model(TestUserInfo.user_test)
        org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
        org_dictionary = org.as_dict()
        user_invitee = factory_user_model(TestUserInfo.user1)
        invitation_info = factory_invitation(org_dictionary['id'])
        new_invitation = InvitationService.create_invitation(invitation_info, User(user_invitee), {}, '').as_dict()
        confirmation_token = InvitationService.generate_confirmation_token(new_invitation['id'])
        InvitationService.accept_invitation(new_invitation['id'], User(user_invitee), '')

        with pytest.raises(BusinessException) as exception:
            InvitationService.validate_token(confirmation_token)

        assert exception.value.code == Error.ACTIONED_INVITATION.name
Ejemplo n.º 25
0
def test_get_invitations_by_org_id(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Find an existing invitation with the provided org id."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        user_with_token = TestUserInfo.user_test
        user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub']
        user = factory_user_model(user_with_token)
        org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
        org_dictionary = org.as_dict()
        org_id = org_dictionary['id']
        invitation_info = factory_invitation(org_dictionary['id'])
        InvitationService.create_invitation(invitation_info, User(user), {}, '').as_dict()
        invitations: list = InvitationService.get_invitations_for_org(org_id,
                                                                      status='PENDING',
                                                                      token_info=TestJwtClaims.public_user_role)
        assert invitations
        assert len(invitations) == 1
Ejemplo n.º 26
0
def test_send_invitation_exception(session, notify_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Send an existing invitation with exception."""
    user = factory_user_model(TestUserInfo.user_test)
    user_dictionary = User(user).as_dict()
    org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
    org_dictionary = org.as_dict()

    invitation_info = factory_invitation(org_dictionary['id'])

    invitation = InvitationModel.create_from_dict(invitation_info, user.id, 'STANDARD')

    with patch.object(notification, 'send_email', return_value=False):
        with pytest.raises(BusinessException) as exception:
            InvitationService.send_invitation(invitation, org_dictionary['name'], user_dictionary, '', '')

    assert exception.value.code == Error.FAILED_INVITATION.name
Ejemplo n.º 27
0
def test_get_invitations(session, auth_mock):  # pylint:disable=unused-argument
    """Assert that invitations for an org can be retrieved."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        user_with_token = TestUserInfo.user_test
        user_with_token['keycloak_guid'] = TestJwtClaims.edit_role['sub']
        user = factory_user_model(user_info=user_with_token)
        org = OrgService.create_org(TestOrgInfo.org1, user.id)
        org_dictionary = org.as_dict()

        invitation_info = factory_invitation(org_dictionary['id'])

        invitation = InvitationService.create_invitation(invitation_info, UserService(user), {}, '')

        response = InvitationService.get_invitations_for_org(org_dictionary['id'], 'PENDING', TestJwtClaims.edit_role)
        assert response
        assert len(response) == 1
        assert response[0].recipient_email == invitation.as_dict()['recipientEmail']
Ejemplo n.º 28
0
def test_get_invitations_by_org_id(session, auth_mock):  # pylint:disable=unused-argument
    """Find an existing invitation with the provided org id."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        user = factory_user_model(TestUserInfo.user_test)
        org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
        org_dictionary = org.as_dict()
        org_id = org_dictionary['id']
        invitation_info = factory_invitation(org_dictionary['id'])
        InvitationService.create_invitation(invitation_info, User(user), {},
                                            '').as_dict()
        invitations: list = InvitationService.get_invitations_by_org_id(
            org_id, 'ALL')
        assert invitations
        assert len(invitations) == 1

        invitations: list = InvitationService.get_invitations_by_org_id(
            org_id, 'PENDING')
        assert len(invitations) == 1
Ejemplo n.º 29
0
def test_accept_invitation_for_govm(session, auth_mock, keycloak_mock,
                                    monkeypatch):  # pylint:disable=unused-argument
    """Accept the invitation and add membership from the invitation to the org."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        with patch.object(auth, 'check_auth', return_value=True):
            with patch.object(InvitationService,
                              'notify_admin',
                              return_value=None):
                user_with_token = TestUserInfo.user_staff_admin
                user_with_token[
                    'keycloak_guid'] = TestJwtClaims.public_user_role['sub']
                user = factory_user_model(user_with_token)

                patch_token_info(TestJwtClaims.staff_admin_role, monkeypatch)

                org = OrgService.create_org(TestOrgInfo.org_govm,
                                            user_id=user.id)
                org_dictionary = org.as_dict()
                invitation_info = factory_invitation(org_dictionary['id'])
                user_with_token_invitee = TestUserInfo.user1
                user_with_token_invitee[
                    'keycloak_guid'] = TestJwtClaims.edit_role_2['sub']
                user_invitee = factory_user_model(user_with_token_invitee)
                new_invitation = InvitationService.create_invitation(
                    invitation_info, User(user_invitee), '')
                new_invitation_dict = new_invitation.as_dict()
                with patch.object(ActivityLogPublisher,
                                  'publish_activity',
                                  return_value=None) as mock_alp:
                    InvitationService.accept_invitation(
                        new_invitation_dict['id'], User(user_invitee), '')
                    mock_alp.assert_called_with(
                        Activity(
                            action=ActivityAction.APPROVE_TEAM_MEMBER.value,
                            org_id=ANY,
                            name=ANY,
                            id=ANY,
                            value=ANY))

                members = MembershipService.get_members_for_org(
                    org_dictionary['id'], 'ACTIVE')
                assert members
                assert len(members) == 1, 'user gets active membership'
Ejemplo n.º 30
0
def test_accept_invitation(session, auth_mock):  # pylint:disable=unused-argument
    """Accept the invitation and add membership from the invitation to the org."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        with patch.object(auth, 'check_auth', return_value=True):
            with patch.object(InvitationService,
                              'notify_admin',
                              return_value=None):
                user = factory_user_model(TestUserInfo.user_test)
                org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id)
                org_dictionary = org.as_dict()
                invitation_info = factory_invitation(org_dictionary['id'])
                new_invitation = InvitationService.create_invitation(
                    invitation_info, User(user), {}, '')
                new_invitation_dict = new_invitation.as_dict()
                InvitationService.accept_invitation(new_invitation_dict['id'],
                                                    User(user), '')
                org_dict = OrgService.find_by_org_id(
                    org_dictionary['id'], allowed_roles={'basic'}).as_dict()
                assert len(
                    org_dict['members']
                ) == 2  # Member count will be 2 only if the invite accept is successful.