Beispiel #1
0
def test_integration_users_endpoint(client):
    """Test integration for GET /users endpoint."""
    user = UserFactory()
    token = get_token(user)
    client.force_login(user=user)
    response = client.get(
        '/users',
        HTTP_AUTHORIZATION='Bearer %s' % token,
    )
    assert response.status_code == 200
    result = json.loads(response.content.decode('utf-8'))
    assert 'meta' in result
    assert result['meta'] == {
        'pagination': {
            'count': 1,
            'page': 1,
            'pages': 1
        },
        'size': 1,
    }
    assert 'data' in result
    assert len(result['data']) == 1
    assert result['data'][0]['type'] == 'users'
    assert result['data'][0]['id'] == str(user.id)
    assert result['data'][0]['attributes'] == {
        'birthday': user.birthday.isoformat(),
        'club': user.club,
        'dominant-hand': user.dominant_hand,
        'email': user.email,
        'first-name': user.first_name,
        'gender': user.gender,
        'last-name': user.last_name,
        'ranking': user.ranking,
    }
Beispiel #2
0
def test_get_list_videopointviewset(client):
    """Test integration for VideoPointViewSet endpoint with client."""
    videopoint = VideoPointFactory()
    user = UserFactory()
    token = get_token(user=user)
    response = client.get(
        reverse('videopoint-list'),
        HTTP_AUTHORIZATION='Bearer %s' % token,
    )
    assert response.status_code == 200
    result = json.loads(response.content)
    assert result['data'] == [
        {
            'attributes': {
                'frame': videopoint.frame,
                'time': '0' + str(videopoint.time),
            },
            'type': 'video-points',
            'id': str(videopoint.id),
        },
    ]
    assert result['meta'] == {
        'pagination': {'count': 1, 'page': 1, 'pages': 1},
    }
    assert result['links']['prev'] is None
    assert result['links']['next'] is None
    assert '/videopoints?page=1' in result['links']['first']
    assert '/videopoints?page=1' in result['links']['last']
Beispiel #3
0
 def post(self, request):
     msg = _('Invalid credentials')
     try:
         email = request.data.get('email')
         password = request.data.get('password')
     except AttributeError:
         raise exceptions.ValidationError(msg)
     try:
         user = User.objects.get(email=email)
     except User.DoesNotExist:
         raise exceptions.ValidationError(msg)
     if not user.check_password(password):
         raise exceptions.ValidationError(msg)
     token = get_token(user)
     user.last_login = datetime.now(timezone.utc)
     user.save()
     user_serializer = UserSerializer(user, context={'request': request})
     response = Response(user_serializer.data)
     response.set_cookie(
         'access_token',
         token,
         secure=not settings.DEBUG,
         httponly=True
     )
     user_serializer.token = token
     return response
Beispiel #4
0
 def post(self, request):
     try:
         provider_serializer = ProviderSerializer(
             data=request.data,
             context={'request': request}
         )
         provider_serializer.is_valid(raise_exception=True)
         provider_serializer.save()
         user = provider_serializer.instance
         token = get_token(user)
         user.last_login = datetime.now(timezone.utc)
         user.save()
         user_serializer = UserSerializer(
             user,
             context={'request': request},
         )
         response = Response(user_serializer.data)
         response.set_cookie(
             'access_token',
             token,
             secure=not settings.DEBUG,
             httponly=True
         )
         user_serializer.token = token
         return response
     except Exception as e:
         raise e
def test_get_list_statsviewset(client):
    """Test integration for StatsViewSet endpoint with client."""
    stats = StatsFactory()
    user = UserFactory()
    token = get_token(user=user)
    response = client.get(
        reverse('stats:stats-list'),
        HTTP_AUTHORIZATION='Bearer %s' % token,
    )
    assert response.status_code == 200
    result = json.loads(response.content)
    assert result['data'] == [
        {
            'attributes': {
                'data': stats.data,
            },
            'type': 'stats',
            'id': str(stats.id),
        },
    ]
    assert result['meta'] == {
        'pagination': {
            'count': 1,
            'page': 1,
            'pages': 1
        },
    }
    assert result['links']['prev'] is None
    assert result['links']['next'] is None
    assert '/stat?page=1' in result['links']['first']
    assert '/stat?page=1' in result['links']['last']
def test_get_list_assetviewset(client):
    """Test integration for /assets endpoint."""
    asset = AssetFactory()
    user = asset.owner
    token = get_token(user=user)
    response = client.get(
        '/assets',
        HTTP_AUTHORIZATION='Bearer %s' % token,
    )
    assert response.status_code == 200
    result = json.loads(response.content)
    assert result['data'] == [
        {
            'attributes': {
                'url': asset.url,
                'info': asset.info,
            },
            'type': 'assets',
            'id': str(asset.id),
        },
    ]
    assert result['meta'] == {
        'pagination': {'count': 1, 'page': 1, 'pages': 1},
    }
    assert result['links']['prev'] is None
    assert result['links']['next'] is None
    assert '/assets?page=1' in result['links']['first']
    assert '/assets?page=1' in result['links']['last']
Beispiel #7
0
 def create(self, request, *args, **kwargs):
     response = super(AuthRegisterView,
                      self).create(request, *args, **kwargs)
     token = get_token(response.data.serializer.instance)
     response.set_cookie('access_token',
                         token,
                         secure=not settings.DEBUG,
                         httponly=True)
     response.data.serializer.token = token
     return response
Beispiel #8
0
def test_integration_users_me_endpoint(client):
    """Test integration for GET /users/me endpoint."""
    user = UserFactory()
    token = get_token(user)
    client.force_login(user=user)
    response = client.get(
        '/users/me',
        HTTP_AUTHORIZATION='Bearer %s' % token,
    )
    assert response.status_code == 200
    result = json.loads(response.content.decode('utf-8'))
    assert result == response_for_user(user, response.wsgi_request)
Beispiel #9
0
def test_get_list_sessionviewset(client):
    """Test integration for SessionViewSet endpoint with client."""
    session = SessionFactory()
    user = UserFactory()
    token = get_token(user=user)
    response = client.get(
        reverse('session-list'),
        HTTP_AUTHORIZATION='Bearer %s' % token,
    )
    request = response.wsgi_request
    assert response.status_code == 200
    result = json.loads(response.content)
    assert result['data'] == [
        {
            'attributes': {
                'data': {
                    'surface': 'clay',
                },
            },
            'relationships': {
                'current-stats': {
                    'data': None,
                    'links': {
                        'related': 'http://testserver/sessions' +
                        '/%d/current_stats' % session.id,
                    },
                },
                'videos': {
                    'data': [],
                    'links': {
                        'related': request.build_absolute_uri(
                            reverse('session-videos-list', args=[session.id]),
                        ),
                    },
                    'meta': {
                        'count': 0,
                    },
                },
            },
            'type': 'sessions',
            'id': str(session.id),
        },
    ]
    assert result['meta'] == {
        'pagination': {'count': 1, 'page': 1, 'pages': 1},
    }
    assert result['links']['prev'] is None
    assert result['links']['next'] is None
    assert '/sessions?page=1' in result['links']['first']
    assert '/sessions?page=1' in result['links']['last']
Beispiel #10
0
def test_get_list_videoviewset(client):
    """Test integration for VideoViewSet endpoint with client."""
    video = VideoFactory()
    user = UserFactory()
    token = get_token(user=user)
    response = client.get(
        reverse('video-list'),
        HTTP_AUTHORIZATION='Bearer %s' % token,
    )
    assert response.status_code == 200
    result = json.loads(response.content)
    assert result['data'] == [
        {
            'attributes': {
            },
            'type': 'videos',
            'id': str(video.id),
            'relationships': {
                'asset': {
                    'data': {
                        'id': str(video.asset_id),
                        'type': 'assets',
                    },
                    'links': {
                        'related': 'http://testserver/videos/%d/asset' %
                        video.id,
                    },
                },
                'session': {
                    'data': {
                        'id': str(video.session_id),
                        'type': 'sessions',
                    },
                    'links': {
                        'related': 'http://testserver/videos/%d/session' %
                        video.id,
                    },
                },
            },
        },
    ]
    assert result['meta'] == {
        'pagination': {'count': 1, 'page': 1, 'pages': 1},
    }
    assert result['links']['prev'] is None
    assert result['links']['next'] is None
    assert '/videos?page=1' in result['links']['first']
    assert '/videos?page=1' in result['links']['last']