-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from JoshuaOndieki/ft-register-vehicle-158860854
[Delivers #158860854] Register Vehicle Endpoint
- Loading branch information
Showing
8 changed files
with
189 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
Controller for endpoints on user | ||
""" | ||
from flask_jwt_extended import get_jwt_identity | ||
|
||
from ridemyway.utils.response import Response | ||
from ridemyway.utils.db_queries import (select_user, | ||
insert_vehicle, select_vehicle) | ||
|
||
|
||
class VehicleController(): | ||
""" | ||
Registers a vehicle | ||
""" | ||
|
||
def register(self, **kwargs): | ||
""" | ||
register vehicle | ||
""" | ||
username = get_jwt_identity() | ||
self.user = select_user(username) | ||
if self.user['usertype'] == 'driver': | ||
vehicle = select_vehicle(kwargs['number_plate']) | ||
if vehicle: | ||
message = 'A vehicle with that numberplate is registered' | ||
return Response.failed(message=message), 409 | ||
kwargs['driver'] = self.user['username'] | ||
reg_vehicle = insert_vehicle(**kwargs) | ||
if reg_vehicle: | ||
message = 'Vehicle registered successfully' | ||
return Response.success(message=message), 201 | ||
message = 'Only drivers can register vehicles' | ||
return Response.failed(message=message), 403 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
Module for testing add vehicle | ||
""" | ||
|
||
import unittest | ||
import json | ||
|
||
from ridemyway.tests.tests_v2 import V2BaseTest | ||
from ridemyway.tests.tests_v2.urls import SIGNUP, LOGIN, VEHICLE | ||
from ridemyway.tests.tests_v2.data import (VALID_RIDER, | ||
VALID_DRIVER, | ||
VALID_VEHICLE, | ||
INVALID_VEHICLE_CAPACITY) | ||
|
||
|
||
class TestCreateVehicle(V2BaseTest): | ||
""" | ||
Tests Add Vehicle API endpoint | ||
- Ride: '/api/v2/vehicle' # POST | ||
""" | ||
|
||
def authenticate(self): | ||
self.client().post(SIGNUP, data=VALID_DRIVER) | ||
self.response = self.client().post(LOGIN, data=VALID_DRIVER) | ||
access_token = json.loads(self.response.data.decode())['access_token'] | ||
return access_token | ||
|
||
def test_add_vehicle_successful(self): | ||
access_token = self.authenticate() | ||
self.response = self.client().post(VEHICLE, data=VALID_VEHICLE, | ||
headers=dict( | ||
Authorization="Bearer " + | ||
access_token)) | ||
self.assertEqual(self.response.status_code, 201, | ||
msg='Should create a vehicle successfully!') | ||
|
||
def test_doesnot_allow_duplicate_vehicles(self): | ||
access_token = self.authenticate() | ||
self.client().post(VEHICLE, data=VALID_VEHICLE, | ||
headers=dict( | ||
Authorization="Bearer " + access_token)) | ||
self.response = self.client().post(VEHICLE, data=VALID_VEHICLE, | ||
headers=dict( | ||
Authorization="Bearer " + | ||
access_token)) | ||
self.assertEqual(self.response.status_code, 409, | ||
msg='Should not add duplicate vehicles!') | ||
|
||
def test_add_vehicle_unsuccessful_with_rider(self): | ||
self.client().post(SIGNUP, data=VALID_RIDER) | ||
self.response = self.client().post(LOGIN, data=VALID_RIDER) | ||
access_token = json.loads(self.response.data.decode())['access_token'] | ||
self.response = self.client().post(VEHICLE, data=VALID_VEHICLE, | ||
headers=dict( | ||
Authorization="Bearer " + | ||
access_token)) | ||
result = json.loads(self.response.data.decode()) | ||
self.assertEqual(result['message'], | ||
'Only drivers can register vehicles', | ||
msg='Only drivers can register vehicles') | ||
self.assertEqual(self.response.status_code, 403) | ||
|
||
def test_does_not_add_vehicle_with_invalid_capacity(self): | ||
access_token = self.authenticate() | ||
|
||
self.response = self.client().post(VEHICLE, data=INVALID_VEHICLE_CAPACITY, | ||
headers=dict( | ||
Authorization="Bearer " + | ||
access_token)) | ||
result = json.loads(self.response.data.decode()) | ||
print(result) | ||
self.assertEqual(result['errors']['capacity'], | ||
'Capacity is in invalid format', | ||
msg='Should only accept integer capacity!') | ||
self.assertEqual(self.response.status_code, 422) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters