def test_activity_list_filter_active(auth_client): user = auth_client.user ActivityFactory.create(user=user) activity = ActivityFactory.create(user=user, to_time=None) url = reverse("activity-list") response = auth_client.get(url, data={"active": "true"}) assert response.status_code == status.HTTP_200_OK json = response.json() assert len(json["data"]) == 1 assert json["data"][0]["id"] == str(activity.id)
def test_activity_list_filter_active(auth_client): user = auth_client.user ActivityFactory.create(user=user) activity = ActivityFactory.create(user=user) ActivityBlockFactory.create(activity=activity, to_time=None) url = reverse('activity-list') response = auth_client.get(url, data={'active': 'true'}) assert response.status_code == status.HTTP_200_OK json = response.json() assert len(json['data']) == 1 assert json['data'][0]['id'] == str(activity.id)
def setUp(self): """Set the environment for the tests up.""" super().setUp() other_user = User.objects.create_user(username='******', password='******') self.activities = ActivityFactory.create_batch(10, user=self.user) for activity in self.activities: ActivityBlockFactory.create_batch(5, activity=activity) ActivityFactory.create_batch(10, user=other_user)
def test_activity_list_filter_day(auth_client): user = auth_client.user day = date(2016, 2, 2) ActivityFactory.create(date=day - timedelta(days=1), user=user) activity = ActivityFactory.create(date=day, user=user) url = reverse("activity-list") response = auth_client.get(url, data={"day": day.strftime("%Y-%m-%d")}) assert response.status_code == status.HTTP_200_OK json = response.json() assert len(json["data"]) == 1 assert json["data"][0]["id"] == str(activity.id)
def setUp(self): """Set the environment for the tests up.""" super().setUp() other_user = get_user_model().objects.create_user(username='******', password='******') activity = ActivityFactory.create(user=self.user) other_activity = ActivityFactory.create(user=other_user) self.activity_blocks = ActivityBlockFactory.create_batch( 10, activity=activity) ActivityBlockFactory.create_batch(10, activity=other_activity)
def test_activity_block_create(auth_client): user = auth_client.user activity = ActivityFactory.create(user=user) data = { 'data': { 'type': 'activity-blocks', 'id': None, 'attributes': { 'from-time': '08:00' }, 'relationships': { 'activity': { 'data': { 'type': 'activities', 'id': activity.id } } } } } url = reverse('activity-block-list') response = auth_client.post(url, data) assert response.status_code == status.HTTP_201_CREATED json = response.json() assert not json['data']['attributes']['from-time'] == '08:00' assert json['data']['attributes']['to-time'] is None
def test_activity_block_to_before_from(auth_client): """Test that to is not before from.""" activity = ActivityFactory.create(user=auth_client.user) block = ActivityBlockFactory.create(from_time=time(7, 30), to_time=None, activity=activity) data = { 'data': { 'type': 'activity-blocks', 'id': block.id, 'attributes': { 'to-time': '07:00', } } } url = reverse('activity-block-detail', args=[block.id]) res = auth_client.patch(url, data) assert res.status_code == status.HTTP_400_BAD_REQUEST json = res.json() assert json['errors'][0]['detail'] == ( 'An activity block may not end before it starts.')
def test_activity_block_active_unique(auth_client): """Should not be able to have two active blocks.""" activity = ActivityFactory.create(user=auth_client.user) block = ActivityBlockFactory.create(to_time=None, activity=activity) data = { 'data': { 'type': 'activity-blocks', 'id': None, 'attributes': { 'from-time': '08:00', }, 'relationships': { 'activity': { 'data': { 'type': 'activities', 'id': block.activity.id } } } } } url = reverse('activity-block-list') res = auth_client.post(url, data) assert res.status_code == status.HTTP_400_BAD_REQUEST json = res.json() assert json['errors'][0]['detail'] == ( 'A user can only have one active activity')
def test_activity_detail(auth_client): activity = ActivityFactory.create(user=auth_client.user) url = reverse('activity-detail', args=[activity.id]) response = auth_client.get(url) assert response.status_code == status.HTTP_200_OK
def test_activity_delete(auth_client): activity = ActivityFactory.create(user=auth_client.user) url = reverse("activity-detail", args=[activity.id]) response = auth_client.delete(url) assert response.status_code == status.HTTP_204_NO_CONTENT
def test_activity_retrievable_not_editable(auth_client): """Test that transferred activities are still retrievable.""" activity = ActivityFactory.create(user=auth_client.user, transferred=True) url = reverse("activity-detail", args=[activity.id]) response = auth_client.get(url) assert response.status_code == status.HTTP_200_OK
def test_activity_set_to_time_none(auth_client): activity = ActivityFactory.create(user=auth_client.user) ActivityFactory.create(user=auth_client.user, to_time=None) data = { "data": { "type": "activities", "id": activity.id, "attributes": { "to-time": None }, } } url = reverse("activity-detail", args=[activity.id]) res = auth_client.patch(url, data) assert res.status_code == status.HTTP_400_BAD_REQUEST
def test_activity_block_delete(auth_client): user = auth_client.user activity = ActivityFactory.create(user=user) block = ActivityBlockFactory.create(activity=activity) url = reverse('activity-block-detail', args=[block.id]) response = auth_client.delete(url) assert response.status_code == status.HTTP_204_NO_CONTENT
def test_activity_list(auth_client): activity = ActivityFactory.create(user=auth_client.user) url = reverse("activity-list") response = auth_client.get(url) assert response.status_code == status.HTTP_200_OK json = response.json() assert len(json["data"]) == 1 assert json["data"][0]["id"] == str(activity.id)
def test_activity_block_list(auth_client): user = auth_client.user activity = ActivityFactory.create(user=user) block = ActivityBlockFactory.create(activity=activity) ActivityBlockFactory.create() url = reverse('activity-block-list') response = auth_client.get(url) assert response.status_code == status.HTTP_200_OK json = response.json() assert len(json['data']) == 1 assert json['data'][0]['id'] == str(block.id)
def test_activity_active_unique(auth_client): """Should not be able to have two active blocks.""" ActivityFactory.create(user=auth_client.user, to_time=None) data = { "data": { "type": "activities", "id": None, "attributes": { "from-time": "08:00", "date": "2017-01-01", "comment": "Test activity", }, } } url = reverse("activity-list") res = auth_client.post(url, data) assert res.status_code == status.HTTP_400_BAD_REQUEST json = res.json() assert json["errors"][0]["detail"] == ( "A user can only have one active activity")
def test_activity_not_editable(auth_client): """Test that transferred activities are read only.""" activity = ActivityFactory.create(user=auth_client.user, transferred=True) data = { "data": { "type": "activities", "id": activity.id, "attributes": { "comment": "Changed Comment" }, } } url = reverse("activity-detail", args=[activity.id]) response = auth_client.patch(url, data) assert response.status_code == status.HTTP_403_FORBIDDEN
def test_activity_active_update(auth_client): activity = ActivityFactory.create(user=auth_client.user, to_time=None) data = { "data": { "type": "activities", "id": activity.id, "attributes": { "from-time": "08:00", "comment": "Changed Comment" }, } } url = reverse("activity-detail", args=[activity.id]) response = auth_client.patch(url, data) assert response.status_code == status.HTTP_200_OK json = response.json() assert (json["data"]["attributes"]["comment"] == data["data"]["attributes"] ["comment"])
def test_activity_update(auth_client): activity = ActivityFactory.create(user=auth_client.user) data = { 'data': { 'type': 'activities', 'id': activity.id, 'attributes': { 'comment': 'Test activity 2' } } } url = reverse('activity-detail', args=[activity.id]) response = auth_client.patch(url, data) assert response.status_code == status.HTTP_200_OK json = response.json() assert (json['data']['attributes']['comment'] == data['data']['attributes'] ['comment'])
def test_activity_block_update(auth_client): user = auth_client.user activity = ActivityFactory.create(user=user) block = ActivityBlockFactory.create(activity=activity, to_time=time(10, 0)) data = { 'data': { 'type': 'activity-blocks', 'id': block.id, 'attributes': { 'to-time': '23:59:00', 'from-time': '10:00:00' } } } url = reverse('activity-block-detail', args=[block.id]) response = auth_client.patch(url, data) assert response.status_code == status.HTTP_200_OK json = response.json() assert (json['data']['attributes']['to-time'] == data['data']['attributes'] ['to-time'])
def test_activity_to_before_from(auth_client): """Test that to is not before from.""" activity = ActivityFactory.create(user=auth_client.user, from_time=time(7, 30), to_time=None) data = { "data": { "type": "activities", "id": activity.id, "attributes": { "to-time": "07:00" }, } } url = reverse("activity-detail", args=[activity.id]) res = auth_client.patch(url, data) assert res.status_code == status.HTTP_400_BAD_REQUEST json = res.json() assert json["errors"][0]["detail"] == ( "An activity block may not end before it starts.")