class ItineraryGetDriverItineraries(AppTestCase):

    def setUp(self):
        super(ItineraryGetDriverItineraries, self).setUp()

        self.test_driver = Driver(email='*****@*****.**')
        Driver.insert(self.test_driver)

        secret_key = self.app.config['SECRET_KEY']
        self.test_driver_token = self.test_driver.generate_token(secret_key)

        self.test_itinerary = Itinerary(id='1', name='test_itinerary', owner='*****@*****.**',
                                        timestart='12:00', endtime='13:00', weekdays='Seg')
        Itinerary.insert(self.test_itinerary)

    def test_get_driver_itineraries(self):
        url = url_for('itinerary.list_driver_itineraries')

        response = self.client.post(url, data=json.dumps({
            'email': '*****@*****.**'
        }), headers={
            'Content-Type': 'application/json',
            'Authorization': self.test_driver_token
        })

        self.assertEqual(response.status_code, 201)
class ItineraryAddPartnerTestCase(AppTestCase):

    def setUp(self):
        super(ItineraryAddPartnerTestCase, self).setUp()

        self.test_driver = Driver(email='*****@*****.**')
        Driver.insert(self.test_driver)

        secret_key = self.app.config['SECRET_KEY']
        self.test_driver_token = self.test_driver.generate_token(secret_key)

        self.test_itinerary = Itinerary(id='1', name='test_itinerary')
        Itinerary.insert(self.test_itinerary)

    def test_add_partner_itinerary(self):
        url = url_for('itinerary.add_partner', itinerary_id='1')

        response = self.client.post(url, data=json.dumps({
            'email': '*****@*****.**',
            'name': 'Driver Test',
            'phone': '912345678'
        }), headers={
            'Content-Type': 'application/json',
            'Authorization': self.test_driver_token
        })

        self.assertEqual(response.status_code, 201)
class ItineraryUpdateDriverLocation(AppTestCase):

    def setUp(self):
        super(ItineraryUpdateDriverLocation, self).setUp()

        self.test_driver = Driver(email='*****@*****.**')
        Driver.insert(self.test_driver)

        secret_key = self.app.config['SECRET_KEY']
        self.test_driver_token = self.test_driver.generate_token(secret_key)

        self.test_itinerary = Itinerary(id='1', name='test_itinerary', owner='*****@*****.**',
                                        timestart='12:00', endtime='13:00', weekdays='Seg')
        Itinerary.insert(self.test_itinerary)

    def test_update_driver_location(self):
        url = url_for('itinerary.update_driver_location', itinerary_id='1')

        response = self.client.post(url, data=json.dumps({
            'city': 'São José dos Campos',
            'latitude': -23.2575824,
            'longitude': -45.8970541,
            'neighborhood': 'Residencial Bosque dos Ipes',
            'state': 'São Paulo',
            'street': 'Rua Luíz Omar da Silva'
        }), headers={
            'Content-Type': 'application/json',
            'Authorization': self.test_driver_token
        })

        self.assertEqual(response.status_code, 201)
class ItineraryCreateViewTestCase(AppTestCase):

    def setUp(self):
        self.test_driver = Driver(email='*****@*****.**')
        Driver.insert(self.test_driver)

        secret_key = self.app.config['SECRET_KEY']
        self.test_driver_token = self.test_driver.generate_token(secret_key)

    def test_create_itinerary(self):
        url = url_for('itinerary.create')

        response = self.client.post(url, data=json.dumps({
            'name': 'IT123',
            'timestart': '06:00',
            'endtime': '07:00',
            'destination': {
                'street': 'Street 1',
                'neighborhood': 'Neigh 1',
                'city': 'City 1',
                'state': 'AB',
                'latitude': -27.4578,
                'longitude': -45.8796
            },
            'weekdays': 'Seg Ter Qua Qui Sex'
        }), headers={
            'Content-Type': 'application/json',
            'Authorization': self.test_driver_token
        })

        self.assertEqual(response.status_code, 201)

        itinerary = Itinerary.find_one({
            'name': 'IT123',
            'timestart': '06:00',
            'endtime': '07:00',
            'destination': {
                'street': 'Street 1',
                'neighborhood': 'Neigh 1',
                'city': 'City 1',
                'state': 'AB',
                'latitude': -27.4578,
                'longitude': -45.8796
            },
            'weekdays': 'Seg Ter Qua Qui Sex',
            'owner': '*****@*****.**',
            'drivers': []
        })

        self.assertIsNotNone(itinerary)
class PassengerViewTestCase(AppTestCase):

    def setUp(self):
        self.test_user = Driver(email='*****@*****.**')
        self.test_user.set_access_type('driver')
        User.insert(self.test_user)

        secret_key = self.app.config['SECRET_KEY']
        self.test_user_token = self.test_user.generate_token(secret_key)

    def test_create_passenger(self):
        url = url_for('passenger.create')

        response = self.client.post(url, data=json.dumps({
            'name': 'passenger1',
            'email': '*****@*****.**',
            'phone': '12345678',
            'boarding_address': {
                'street': 'Street 1',
                'neighborhood': 'Neigh 1',
                'city': 'City 1',
                'state': 'AB',
                'lat': -27.4578,
                'lng': -45.8796
            }
        }), headers={
            'Content-Type': 'application/json',
            'Authorization': self.test_user_token
        })

        self.assertEqual(response.status_code, 201)

        passenger = Passenger.find_one({
            'name': 'passenger1',
            'email': '*****@*****.**',
            'phone': '12345678',
            'boarding_address': {
                'street': 'Street 1',
                'neighborhood': 'Neigh 1',
                'city': 'City 1',
                'state': 'AB',
                'lat': -27.4578,
                'lng': -45.8796
            }
        })

        self.assertIsNotNone(passenger)