Example #1
0
def test__user_detail__returns_user_detail(calendar_user):
    """Tests that proper response is returned."""
    # Create token and add user permissions
    token = create_token(calendar_user.sb_user)
    utils.add_api_permission(calendar_user.sb_user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get(
        reverse('api:rdrhc_calendar_v1:user_detail',
                kwargs={'user_id': calendar_user.id}))
    content = json.loads(response.content)

    # Confirm types returned
    assert isinstance(content['id'], int)
    assert isinstance(content['sb_user'], int)
    assert isinstance(content['name'], str)
    assert isinstance(content['schedule_name'], str)
    assert isinstance(content['calendar_name'], str)
    assert isinstance(content['role'], str)
    assert isinstance(content['first_email_sent'], bool)
    assert isinstance(content['full_day'], bool)
    assert isinstance(content['reminder'], int)

    # Confirm right model instance is pulled
    assert content['id'] == calendar_user.id
Example #2
0
def test__upload_hc_dpd_data__valid_post(user):
    """Tests that proper response is returned."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_edit_permission(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.post(
        reverse('api:hc_dpd_v1:upload_hc_dpd_data'),
        data=utils.UPLOAD_ALL_DATA,
        format='json',
    )
    content = json.loads(response.content)

    # Confirm status code
    assert response.status_code == 201

    # Confirm response details are received as expected
    assert 'message' in content
    assert isinstance(content['message'], list)
    assert len(content['message']) == 13

    assert 'status_code' in content
    assert content['status_code'] == 201
Example #3
0
def test__upload_hc_dpd_data__201_response_on_user_with_edit_permissions(user):
    """Test for 201 response on user with view permission."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_edit_permission(user)

    # Setup Data
    data = {
        'data': [{
            'drug_code': 1,
            'extract_data': {
                'active_ingredient': [{'drug_code': 1, 'active_ingredient_code': 'A'}],
            },
        }]
    }

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.post(
        reverse('api:hc_dpd_v1:upload_hc_dpd_data'),
        data=data,
        format='json',
    )

    assert response.status_code == 201
Example #4
0
def test__upload_hc_dpd_data__invalid_post___no_data(user):
    """Tests that proper response is returned for no post data."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_edit_permission(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.post(
        reverse('api:hc_dpd_v1:upload_hc_dpd_data'),
        data={},
        format='json',
    )
    content = json.loads(response.content)

    # Confirm status code
    assert response.status_code == 400

    # Confirm response details are received as expected
    assert 'errors' in content
    assert isinstance(content['errors'], dict)
    assert 'non_field' in content['errors']
    assert isinstance(content['errors']['non_field'], list)
    assert len(content['errors']['non_field']) == 0
    assert 'field' in content['errors']
    assert isinstance(content['errors']['field'], dict)
    assert content['errors']['field'] == {'data': ['This field is required.']}

    assert 'status_code' in content
    assert content['status_code'] == 400
Example #5
0
def test__checksum_test__invalid_data(user):
    """Tests proper response is returned for invalid data."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_view_permission(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.post(
        reverse('api:hc_dpd_v1:checksum_test'),
        data={'active_ingredient': [{'drug_code': 'ERROR'}]},
        format='json',
    )
    content = json.loads(response.content)

    # Confirm status code
    assert response.status_code == 400
    assert content['status_code'] == 400

    # Confirm response details
    assert 'errors' in content
    assert content['errors']['non_field'] == []
    assert content['errors']['field'] == {
        'active_ingredient': [{'drug_code': ['A valid integer is required.']}],
    }
Example #6
0
def test__user_schedule_upload__400_response_on_invalid_data(calendar_user):
    """Confirms error handling with invalid data."""
    # Create token and add user permissions
    user = calendar_user.sb_user
    token = create_token(user)
    utils.add_api_permission(user)

    # Setup POST data
    data = {
        'schedule': [
            {
                'sb_user': user.id,
                'date': '2018-02-01',
                'shift_code': 'abc',
                'text_shift_code': 'A3'
            },
        ]
    }

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.post(
        reverse('api:rdrhc_calendar_v1:user_schedule_upload',
                kwargs={'user_id': token.user.id}),
        json.dumps(data),
        content_type='application/json',
    )
    content = json.loads(response.content)

    assert response.status_code == 400
    assert 'errors' in content
Example #7
0
def test__upload_hc_dpd_data__accessible_by_url(user):
    """Tests that endpoint exists at expected URL."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_edit_permission(user)

    # Setup Data
    data = {
        'data': [{
            'drug_code': 1,
            'extract_data': {
                'active_ingredient': [{'drug_code': 1, 'active_ingredient_code': 'A'}],
            },
        }]
    }

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.post(
        '/api/hc-dpd/v1/upload/',
        data=data,
        format='json',
    )

    assert response.status_code == 201
Example #8
0
def test__user_list__403_response_on_user_without_permissions(user):
    """Test for 403 response on user without permission."""
    # Create token
    token = create_token(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get(reverse('api:rdrhc_calendar_v1:user_list'))

    assert response.status_code == 403
Example #9
0
def test__user_list__accessible_by_url(user):
    """Tests that endpoint exists at expected URL."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_permission(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get('/api/rdrhc-calendar/v1/users/')

    assert response.status_code == 200
Example #10
0
def test__checksum_list__accessible_by_url(user):
    """Tests that endpoint exists at expected URL."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_view_permission(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get('/api/hc-dpd/v1/checksum/?step=1&source=active_ingredient')

    assert response.status_code == 200
Example #11
0
def test__user_list__200_response_on_user_with_permissions(user):
    """Test for 200 response on user with permission."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_permission(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get(reverse('api:rdrhc_calendar_v1:user_list'))

    assert response.status_code == 200
Example #12
0
def test__user_email_list__403_response_on_user_without_permissions(user):
    """Test for 403 response on user without permission."""
    # Create token
    token = create_token(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get(
        reverse('api:rdrhc_calendar_v1:user_email_list',
                kwargs={'user_id': token.user.id}))

    assert response.status_code == 403  # pylint: disable=no-member
Example #13
0
def test__user_email_first_sent__accessible_by_url(calendar_user):
    """Tests that endpoint exists at expected URL."""
    # Create token and add user permissions
    token = create_token(calendar_user.sb_user)
    utils.add_api_permission(calendar_user.sb_user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.post(
        f'/api/rdrhc-calendar/v1/users/{token.user.id}/emails/first-sent/')

    assert response.status_code == 200
Example #14
0
def test__user_schedule_upload__403_response_on_user_without_permissions(user):
    """Test for 403 response on user without permission."""
    # Create token and add user permissions
    token = create_token(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get(
        reverse('api:rdrhc_calendar_v1:user_schedule_upload',
                kwargs={'user_id': token.user.id}))

    assert response.status_code == 403
Example #15
0
def test__user_schedule_delete__204_response_on_user_with_permissions(user):
    """Test for 200 response on user with permission."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_permission(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.delete(
        reverse('api:rdrhc_calendar_v1:user_schedule_delete',
                kwargs={'user_id': token.user.id}))

    assert response.status_code == 204
Example #16
0
def test__checksum_list__403_response_on_user_without_permissions(user):
    """Test for 403 response on user without permission."""
    # Create token
    token = create_token(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get(
        reverse('api:hc_dpd_v1:checksum_list'),
        data={'step': 1, 'source': 'active_ingredient'},
    )

    assert response.status_code == 403
Example #17
0
def test__upload_hc_dpd_data__403_response_on_user_without_permissions(user):
    """Test for 403 response on user without permission."""
    # Create token
    token = create_token(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.post(
        reverse('api:hc_dpd_v1:upload_hc_dpd_data'),
        data={'active_ingredient': [{'drug_code': 1, 'active_ingredient_code': 'A'}]},
        format='json',
    )

    assert response.status_code == 403
Example #18
0
def test_api_returns_user_shift_code_list__default_codes(shift_code):
    """Tests that default codes are returned when applicable."""
    sb_user = shift_code.sb_user

    # Create token and add user permissions
    token = create_token(sb_user)
    utils.add_api_permission(sb_user)

    # Add a calendar to the user
    CalendarUser.objects.create(
        sb_user=sb_user,
        role='p',
    )

    # Setup shift code to function as a default code (no user, same role)
    shift_code.sb_user = None
    shift_code.role = 'p'
    shift_code.save()

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get(
        reverse('api:rdrhc_calendar_v1:user_shift_codes_list',
                kwargs={'user_id': token.user.id}))
    content = json.loads(response.content)

    # Confirm types and value returned
    assert isinstance(content, list)
    assert isinstance(content[0]['id'], int)
    assert isinstance(content[0]['code'], str)
    assert isinstance(content[0]['monday_start'], str)
    assert isinstance(content[0]['monday_duration'], str)
    assert isinstance(content[0]['tuesday_start'], str)
    assert isinstance(content[0]['tuesday_duration'], str)
    assert isinstance(content[0]['wednesday_start'], str)
    assert isinstance(content[0]['wednesday_duration'], str)
    assert isinstance(content[0]['thursday_start'], str)
    assert isinstance(content[0]['thursday_duration'], str)
    assert isinstance(content[0]['friday_start'], str)
    assert isinstance(content[0]['friday_duration'], str)
    assert isinstance(content[0]['saturday_start'], str)
    assert isinstance(content[0]['saturday_duration'], str)
    assert isinstance(content[0]['sunday_start'], str)
    assert isinstance(content[0]['sunday_duration'], str)
    assert isinstance(content[0]['stat_start'], str)
    assert isinstance(content[0]['stat_duration'], str)
    assert content[0]['id'] == shift_code.id
Example #19
0
def test__checksum_test__accessible_by_url(user):
    """Tests that endpoint exists at expected URL."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_edit_permission(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.post(
        '/api/hc-dpd/v1/checksum/test/',
        {'active_ingredient': [{'drug_code': 1, 'active_ingredient_code': 'A'}]},
        format='json',
    )

    assert response.status_code == 200
Example #20
0
def test__checksum_test__200_response_on_user_with_view_permissions(user):
    """Test for 200 response on user with view permission."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_view_permission(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.post(
        reverse('api:hc_dpd_v1:checksum_test'),
        data={'active_ingredient': [{'drug_code': 1, 'active_ingredient_code': 'A'}]},
        format='json',
    )

    assert response.status_code == 200
Example #21
0
def test__user_schedule_upload__json_error(calendar_user):
    """Tests that handling of JSON error."""
    # Create token and add user permissions
    user = calendar_user.sb_user
    token = create_token(user)
    utils.add_api_permission(user)

    # Confirm no shifts exist
    assert Shift.objects.filter(sb_user=user).count() == 0

    # Setup POST data
    data = {
        'schedule': [{
            'sb_user': user.id,
            'date': '2018-02-01',
            'shift_code': '',
            'text_shift_code': 'A3'
        }, {
            'sb_user': user.id,
            'date': '2018-02-02',
            'shift_code': '',
            'text_shift_code': 'A4'
        }, {
            'sb_user': user.id,
            'date': '2018-02-03',
            'shift_code': '',
            'text_shift_code': 'A5'
        }]
    }
    json_str = json.dumps(data)
    json_error = f'<{json_str}>'

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.post(
        reverse('api:rdrhc_calendar_v1:user_schedule_upload',
                kwargs={'user_id': token.user.id}),
        json_error,
        content_type='application/json',
    )

    #  Confirm error status code
    assert response.status_code == 400

    # Confirm error response
    assert 'JSON parse error' in str(response.content)
Example #22
0
def test__user_schedule_delete__deletes_schedule(shift):
    """Tests that proper response is returned."""
    # Confirm shift exists
    assert Shift.objects.filter(sb_user=shift.sb_user).count() == 1

    # Create token and add user permissions
    token = create_token(shift.sb_user)
    utils.add_api_permission(shift.sb_user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    client.delete(
        reverse('api:rdrhc_calendar_v1:user_schedule_delete',
                kwargs={'user_id': token.user.id}))

    assert Shift.objects.filter(sb_user=shift.sb_user).count() == 0
Example #23
0
def test__missing_shift_code_upload__400_response_on_invalid_data(user):
    """Tests that missing shift code results in 400 response with invalid data."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_permission(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.post(
        reverse('api:rdrhc_calendar_v1:missing_shift_codes_upload'),
        json.dumps({'codes': [{
            'shift_code': 'abc'
        }]}),
        content_type='application/json',
    )
    json.loads(response.content)

    assert response.status_code == 400
Example #24
0
def test__stat_holiday_list__returns_list_without_parameters(user):
    """Tests that proper response is returned."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_permission(user)

    # Create stat holidsy
    utils.create_stat_holidays()

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get(reverse('api:rdrhc_calendar_v1:stat_holidays_list'))
    content = json.loads(response.content)

    # Confirm types and value returned
    assert isinstance(content, list)
    assert len(content) == 10
    assert isinstance(content[0], str)
Example #25
0
def test__user_email_first_sent__confirm_change(calendar_user):
    """Tests that proper response is returned."""
    # Confirm value start off false
    assert calendar_user.first_email_sent is False

    # Create token and add user permissions
    token = create_token(calendar_user.sb_user)
    utils.add_api_permission(calendar_user.sb_user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    client.post(
        reverse('api:rdrhc_calendar_v1:user_email_first_sent',
                kwargs={'user_id': token.user.id}))

    # Retrieve updated database entry and confirm its value
    calendar_user.refresh_from_db()
    assert calendar_user.first_email_sent is True
Example #26
0
def test__user_schedule_upload__uploads_user_schedule(calendar_user):
    """Tests that proper response is returned."""
    # Create token and add user permissions
    user = calendar_user.sb_user
    token = create_token(user)
    utils.add_api_permission(user)

    # Confirm no shifts exist
    assert Shift.objects.filter(sb_user=user).count() == 0

    # Setup POST data
    data = {
        'schedule': [{
            'sb_user': user.id,
            'date': '2018-02-01',
            'shift_code': '',
            'text_shift_code': 'A3'
        }, {
            'sb_user': user.id,
            'date': '2018-02-02',
            'shift_code': '',
            'text_shift_code': 'A4'
        }, {
            'sb_user': user.id,
            'date': '2018-02-03',
            'shift_code': '',
            'text_shift_code': 'A5'
        }]
    }

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    client.post(
        reverse('api:rdrhc_calendar_v1:user_schedule_upload',
                kwargs={'user_id': token.user.id}),
        json.dumps(data),
        content_type='application/json',
    )

    # Confirm shift exists
    assert Shift.objects.filter(sb_user=user).count() == 3
Example #27
0
def test__user_email_list__returns_user_email_list(user):
    """Tests that proper response is returned."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_permission(user)

    # Add email for user
    EmailAddress.objects.create(user=user, email='*****@*****.**')

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get(
        reverse('api:rdrhc_calendar_v1:user_email_list',
                kwargs={'user_id': token.user.id}))
    content = json.loads(response.content)  # pylint: disable=no-member

    # Confirm types and value returned
    assert isinstance(content, list)
    assert content[0] == '*****@*****.**'
Example #28
0
def test__shift_list__returns_shift_list(shift):
    """Tests that proper response is returned."""
    # Create token and add user permissions
    token = create_token(shift.sb_user)
    utils.add_api_permission(shift.sb_user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get(reverse('api:rdrhc_calendar_v1:shift_list'))
    content = json.loads(response.content)

    # Confirm types and value returned
    assert isinstance(content, list)
    assert isinstance(content[0]['id'], int)
    assert isinstance(content[0]['sb_user'], int)
    assert isinstance(content[0]['date'], str)
    assert isinstance(content[0]['shift_code'], int)
    assert isinstance(content[0]['text_shift_code'], str)
    assert content[0]['id'] == shift.id
Example #29
0
def test__upload_hc_dpd_data__invalid_method(user):
    """Tests that proper response is returned for invalid method (GET)."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_edit_permission(user)

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get(
        reverse('api:hc_dpd_v1:upload_hc_dpd_data'),
        data=utils.UPLOAD_ALL_DATA,
        format='json',
    )
    content = json.loads(response.content)

    # Confirm status code
    assert response.status_code == 405

    # Confirm message
    assert content == {'detail': 'Method "GET" not allowed.'}
Example #30
0
def test__stat_holiday_list__returns_list_with_parameters(user):
    """Tests that proper response is returned when parameters added."""
    # Create token and add user permissions
    token = create_token(user)
    utils.add_api_permission(user)

    # Create stat holidsy
    utils.create_stat_holidays()

    # Set up client and response
    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION=f'Token {token}')
    response = client.get(reverse('api:rdrhc_calendar_v1:stat_holidays_list'),
                          {
                              'date_start': '2014-01-01',
                              'date_end': '2018-12-31'
                          })
    content = json.loads(response.content)

    # Confirm proper amount of dates returned
    assert len(content) == 5