Example #1
0
    def test_list(self):
        StationFactory()

        response = self.client.get(self.url_list)
        response = response.json()

        self.assertEquals(len(response), 1)
Example #2
0
    def test_list(self):
        StationFactory()

        response = self.client.get(self.url)
        response = response.json()

        self.assertEquals(response['body'].get('count'), 1)
Example #3
0
    def setUp(self):
        """
        Initialize the objects to use in tests for Station endpoints.
        In stations app.
        """

        self.user = UserFactory()
        self.user_token = TokenFactory(user=self.user)

        self.client.credentials(
            HTTP_AUTHORIZATION="Urbvan {}".format(self.user_token.key))
        self.location = LocationFactory(owner=self.user)
        self.station = StationFactory(location=self.location, owner=self.user)

        self.url = reverse("api_v1_stations:v1_retrieve_station",
                           kwargs={'pk': self.station.id})
Example #4
0
 def test_update_successfully(self):
     """
         Update unit test for Station Model API
     """
     st = StationFactory()
     url = reverse("stations:v1_detail_station", kwargs={'pk': st.id})
     response = self.client.get(url)
     self.assertEquals(response.status_code, 200)
     self.assertEquals(response.data['order'], st.order)
     # Test partial update
     data = {
         "order": st.order + 1,
     }
     response = self.client.patch(url, data=data)
     self.assertEquals(response.status_code, 200)
     # Test total update
     data = {
         "location": st.location.id,
         "route": st.route.id,
         "order": st.order + 2,
         "partial": True
     }
     response = self.client.put(url, data=data)
     self.assertEquals(response.status_code, 200)
     self.assertEquals(response.data['id'], st.id)
     self.assertEquals(response.data['order'], st.order + 2)
Example #5
0
    def test_list(self):
        RouteFactory.create(stations=(StationFactory(), ))

        response = self.client.get(self.url)
        response = response.json()
        print(response)

        self.assertEquals(response['body'].get('count'), 1)
Example #6
0
    def test_retrieve(self):
        station = StationFactory()

        url = reverse("stations:v1_station_manage", kwargs={'pk': station.id})

        response = self.client.get(url, format='json')

        self.assertEquals(response.status_code, 200)
        self.assertEquals(response.data['id'], station.id)
Example #7
0
    def test_retrieve(self):
        route = RouteFactory.create(stations=(StationFactory(), ))

        url = reverse("routes:v1_route_manage", kwargs={'pk': route.id})

        response = self.client.get(url, format='json')

        self.assertEquals(response.status_code, 200)
        self.assertEquals(response.data['id'], route.id)
Example #8
0
 def test_retrieve_successfully(self):
     """
         Retrieve test for Station Model API
     """
     st = StationFactory()
     url = reverse("stations:v1_detail_station", kwargs={'pk': st.id})
     response = self.client.get(url)
     self.assertEquals(response.status_code, 200)
     self.assertEquals(response.data['id'], st.id)
Example #9
0
    def test_get_all_stations(self):
        """Test api can all stations."""

        for _ in range(3):
            StationFactory(owner=self.user, location=self.location)

        response = self.client.get(self.url)
        response = response.json()

        self.assertEquals(response['body'].get('count'), 3)
Example #10
0
    def test_delete(self):
        route = RouteFactory.create(stations=(StationFactory(), ))

        url = reverse("routes:v1_route_manage", kwargs={'pk': route.id})

        response = self.client.delete(url, format='json')

        self.assertEquals(response.status_code, 204)

        deleted = self.client.get(url, format='json')
        self.assertEquals(deleted.status_code, 404)
Example #11
0
    def test_update(self):
        route = RouteFactory.create(stations=(StationFactory(), ))

        data = {"direction": False}

        url = reverse("routes:v1_route_manage", kwargs={'pk': route.id})

        response = self.client.patch(url, data, format='json')

        self.assertEquals(response.status_code, 200)
        self.assertEquals(response.data['direction'], False)
Example #12
0
    def test_update(self):
        station = StationFactory()

        data = {"order": 420}

        url = reverse("stations:v1_station_manage", kwargs={'pk': station.id})

        response = self.client.patch(url, data, format='json')

        self.assertEquals(response.status_code, 200)
        self.assertEquals(response.data['order'], 420)
Example #13
0
    def test_detail_forbidden(self):
        self.user = UserFactory()
        self.user_token = TokenFactory(user=self.user)

        self.client.credentials(
            HTTP_AUTHORIZATION="Urbvan {}".format(self.user_token.key))
        station = StationFactory()
        data = {"pk": station.id}
        url = str(self.url_detail).replace("pk", data["pk"])
        response = self.client.get(url, data, format="json")
        self.assertEquals(response.status_code, 403)
Example #14
0
    def test_delete(self):
        station = StationFactory()

        url = reverse("stations:v1_station_manage", kwargs={'pk': station.id})

        response = self.client.delete(url, format='json')

        self.assertEquals(response.status_code, 204)

        deleted = self.client.get(url, format='json')
        self.assertEquals(deleted.status_code, 404)
Example #15
0
 def test_destroy_successfully(self):
     """
         Update unit test for Station Model API
     """
     st = StationFactory()
     url = reverse("stations:v1_detail_station", kwargs={'pk': st.id})
     response = self.client.get(url)
     self.assertEquals(response.status_code, 200)
     response = self.client.delete(url)
     self.assertEquals(response.status_code, 204)
     response = self.client.get(url)
     self.assertEquals(response.status_code, 404)
Example #16
0
    def test_delete_a_station(self):
        """Test api can delete a station."""

        StationFactory(location=LocationFactory())

        stations = Station.objects.all()
        station_id = self.station.id
        response = self.client.delete(self.url, format='json')
        deleted_station = Station.objects.filter(id=station_id).exists()

        self.assertEquals(response.status_code, status.HTTP_204_NO_CONTENT)
        self.assertTrue(len(stations) == 1)
        self.assertFalse(deleted_station)
Example #17
0
    def test_create_successfully(self):
        line = LineFactory()
        station1 = StationFactory()

        data = {
            "line": line.id,
            "stations": [
                station1.id,
            ],
            "direction": True,
            "is_active": True
        }

        response = self.client.post(self.url, data, format='json')
        self.assertEquals(response.status_code, 201)
Example #18
0
    def test_list(self):
        data = {
            "name": "Urbvan",
            "latitude": 19.388401,
            "longitude": -99.227358
        }

        location = LocationModel(name=data['name'],
                                 latitude=data['latitude'],
                                 longitude=data['longitude'])
        location.save()
        StationFactory(location)

        response = self.client.get(self.url)
        response = response.json()

        self.assertEquals(response['body'].get('count'), 1)
Example #19
0
    def test_create_forbidden(self):
        self.user = UserFactory()
        self.user_token = TokenFactory(user=self.user)
        self.client.credentials(
            HTTP_AUTHORIZATION="Urbvan {}".format(self.user_token))

        line = LineFactory()
        station = StationFactory()

        data = {
            "line": line.id,
            "stations": [station.id],
            "direction": True,
            "is_active": True,
        }

        response = self.client.post(self.url_create, data, format="json")
        self.assertEquals(response.status_code, 403)
Example #20
0
    def test_delete_successfully(self):
        self.user = UserSuperadminFactory()
        self.user_token = TokenFactory(user=self.user)

        self.client.credentials(
            HTTP_AUTHORIZATION="Urbvan {}".format(self.user_token.key))
        station = StationFactory()
        data = {"pk": station.id}

        # Get with success the element
        url = str(self.url_detail).replace("pk", data["pk"])
        response = self.client.get(url, data, format="json")
        self.assertEquals(response.status_code, 200)

        # Delete element and check
        url = str(self.url_delete).replace("pk", data["pk"])
        response = self.client.delete(url, data, format="json")
        self.assertEquals(response.status_code, 204)

        # Not found element
        url = str(self.url_detail).replace("pk", data["pk"])
        response = self.client.get(url, data, format="json")
        self.assertEquals(response.status_code, 404)
Example #21
0
class StationDetailEndpointTest(APITestCase):
    def setUp(self):
        """
        Initialize the objects to use in tests for Station endpoints.
        In stations app.
        """

        self.user = UserFactory()
        self.user_token = TokenFactory(user=self.user)

        self.client.credentials(
            HTTP_AUTHORIZATION="Urbvan {}".format(self.user_token.key))
        self.location = LocationFactory(owner=self.user)
        self.station = StationFactory(location=self.location, owner=self.user)

        self.url = reverse("api_v1_stations:v1_retrieve_station",
                           kwargs={'pk': self.station.id})

    def test_get_a_station(self):
        """Test api get a station."""

        response = self.client.get(self.url, format='json')

        self.assertEquals(response.status_code, status.HTTP_200_OK)
        self.assertContains(response, self.station.id)

    def test_put_a_station(self):
        """Test api can update a station."""

        data = {
            "location": LocationFactory().id,
            "order": 12,
            "is_active": False,
            "owner": self.user.id
        }

        response = self.client.put(self.url, data, format='json')
        new_order_station = response.data['body']['results'][0]['order']
        self.station.refresh_from_db()

        self.assertEquals(response.status_code, status.HTTP_200_OK)
        self.assertEquals(new_order_station, 12)
        self.assertEquals(self.station.order, new_order_station)

    def test_patch_a_station(self):
        """Test api can partial update a station."""

        data = {
            "is_active": False,
        }

        response = self.client.patch(self.url, data, format='json')
        new_state_station = response.data['body']['results'][0]['is_active']
        self.station.refresh_from_db()

        self.assertEquals(response.status_code, status.HTTP_200_OK)
        self.assertEquals(new_state_station, False)
        self.assertEquals(self.station.is_active, new_state_station)

    def test_delete_a_station(self):
        """Test api can delete a station."""

        StationFactory(location=LocationFactory())

        stations = Station.objects.all()
        station_id = self.station.id
        response = self.client.delete(self.url, format='json')
        deleted_station = Station.objects.filter(id=station_id).exists()

        self.assertEquals(response.status_code, status.HTTP_204_NO_CONTENT)
        self.assertTrue(len(stations) == 1)
        self.assertFalse(deleted_station)