def test_org_user_registers_with_email_already_in_use(
    client,
    sample_org_invite,
    mock_email_is_already_in_use,
    mock_get_user_by_email,
    mock_accept_org_invite,
    mock_add_user_to_organisation,
    mock_send_already_registered_email,
    mock_register_user
):
    invited_org_user = InvitedOrgUser(**sample_org_invite)
    with client.session_transaction() as session:
        session['invited_org_user'] = invited_org_user.serialize()

    response = client.post(url_for('main.register_from_org_invite'), data={
        'name': 'Test User',
        'mobile_number': '+4407700900460',
        'password': '******',
        'email_address': session['invited_org_user']['email_address'],
        'organisation': session['invited_org_user']['organisation']
    })

    assert response.status_code == 302
    assert response.location == url_for('main.verify', _external=True)

    mock_get_user_by_email.assert_called_once_with(
        session['invited_org_user']['email_address']
    )
    assert mock_register_user.called is False
    assert mock_send_already_registered_email.called is False
Beispiel #2
0
def test_registration_from_org_invite_has_bad_data(client, sample_org_invite,
                                                   data, error):
    invited_org_user = InvitedOrgUser(**sample_org_invite)
    with client.session_transaction() as session:
        session['invited_org_user'] = invited_org_user.serialize()

    response = client.post(url_for('main.register_from_org_invite'), data=data)

    assert response.status_code == 200
    assert error in response.get_data(as_text=True)
Beispiel #3
0
def invited_user(_id='1234',
                 service=None,
                 from_user='******',
                 email_address='*****@*****.**',
                 permissions=None,
                 status='pending',
                 created_at=datetime.utcnow(),
                 auth_type='sms_auth',
                 organisation=None):
    org_user = organisation is not None
    data = {
        'id': _id,
        'from_user': from_user,
        'email_address': email_address,
        'status': status,
        'created_at': created_at,
        'auth_type': auth_type,
    }
    if service:
        data['service'] = service
    if permissions:
        data['permissions'] = permissions
    if organisation:
        data['organisation'] = organisation

    if org_user:
        return InvitedOrgUser(data['id'], data['organisation'],
                              data['from_user'], data['email_address'],
                              data['status'], data['created_at'])
 def create_invite(self, invite_from_id, org_id, email_address):
     data = {
         'email_address': email_address,
         'invited_by': invite_from_id,
         'invite_link_host': self.admin_url,
     }
     data = _attach_current_user(data)
     resp = self.post(url='/organisation/{}/invite'.format(org_id),
                      data=data)
     return InvitedOrgUser(**resp['data'])
Beispiel #5
0
def test_org_user_registration(
    client,
    sample_org_invite,
    mock_email_is_not_already_in_use,
    mock_register_user,
    mock_send_verify_code,
    mock_get_user_by_email,
    mock_send_verify_email,
    mock_accept_org_invite,
    mock_add_user_to_organisation,
):
    invited_org_user = InvitedOrgUser(**sample_org_invite)
    with client.session_transaction() as session:
        session['invited_org_user'] = invited_org_user.serialize()

    response = client.post(url_for('main.register_from_org_invite'),
                           data={
                               'name':
                               'Test User',
                               'email_address':
                               session['invited_org_user']['email_address'],
                               'mobile_number':
                               '+4407700900460',
                               'password':
                               '******',
                               'organisation':
                               session['invited_org_user']['organisation']
                           })

    assert response.status_code == 302
    assert response.location == url_for('main.verify', _external=True)

    mock_get_user_by_email.called is False
    mock_register_user.assert_called_once_with(
        'Test User', session['invited_org_user']['email_address'],
        '+4407700900460', 'validPassword!', 'sms_auth')
    mock_send_verify_code.assert_called_once_with(
        '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6',
        'sms',
        '+4407700900460',
    )
def test_registration_from_org_invite_has_different_email_or_organisation(
    client,
    sample_org_invite,
    diff_data
):
    invited_org_user = InvitedOrgUser(**sample_org_invite)
    with client.session_transaction() as session:
        session['invited_org_user'] = invited_org_user.serialize()

    for data in diff_data:
        session['invited_org_user'][data] = 'different'

    response = client.post(url_for('main.register_from_org_invite'), data={
        'name': 'Test User',
        'mobile_number': '+4407700900460',
        'password': '******',
        'email_address': session['invited_org_user']['email_address'],
        'organisation': session['invited_org_user']['organisation']
    })

    assert response.status_code == 400
Beispiel #7
0
def test_invite_org_user(
    logged_in_client,
    mocker,
    mock_get_organisation,
    sample_org_invite,
):

    mock_invite_org_user = mocker.patch(
        'app.org_invite_api_client.create_invite',
        return_value=InvitedOrgUser(**sample_org_invite))

    logged_in_client.post(url_for('.invite_org_user', org_id=ORGANISATION_ID),
                          data={'email_address': '*****@*****.**'})

    mock_invite_org_user.assert_called_once_with(
        sample_org_invite['invited_by'],
        '{}'.format(ORGANISATION_ID),
        '*****@*****.**',
    )
def test_verified_org_user_redirects_to_dashboard(
    client,
    sample_org_invite,
    mock_check_verify_code,
    mock_get_user,
    mock_activate_user,
    mock_login,
):
    invited_org_user = InvitedOrgUser(**sample_org_invite).serialize()
    with client.session_transaction() as session:
        session['expiry_date'] = str(datetime.utcnow() + timedelta(hours=1))
        session['user_details'] = {"email": invited_org_user['email_address'], "id": invited_org_user['id']}
        session['organisation_id'] = invited_org_user['organisation']

    response = client.post(url_for('main.verify'), data={'sms_code': '12345'})

    assert response.status_code == 302
    assert response.location == url_for(
        'main.organisation_dashboard',
        org_id=invited_org_user['organisation'],
        _external=True
    )
Beispiel #9
0
def test_invite_org_user_errors_when_same_email_as_inviter(
    client_request,
    mocker,
    mock_get_organisation,
    sample_org_invite,
):
    new_org_user_data = {
        'email_address': '*****@*****.**',
    }

    mock_invite_org_user = mocker.patch(
        'app.org_invite_api_client.create_invite',
        return_value=InvitedOrgUser(**sample_org_invite))

    page = client_request.post('.invite_org_user',
                               org_id=ORGANISATION_ID,
                               _data=new_org_user_data,
                               _follow_redirects=True)

    assert mock_invite_org_user.called is False
    assert normalize_spaces(page.select_one(
        '.error-message').text) == 'You can’t send an invitation to yourself'
 def _get_invited_org_users(self, invites):
     invited_users = []
     for invite in invites:
         invited_user = InvitedOrgUser(**invite)
         invited_users.append(invited_user)
     return invited_users
 def check_token(self, token):
     resp = self.get(url='/invite/organisation/{}'.format(token))
     return InvitedOrgUser(**resp['data'])