Skip to content

Commit

Permalink
feat: vehicle has permission (#59)
Browse files Browse the repository at this point in the history
* feat: has permission

* chore: moved location of has permission

* removed redundant print

* multi has permissions

* chore: function name change

* chore: left something in tests

* chore: docstring changes

* chore: fixed false

* chore: e2e tests

* chore: removed semicolon

* chore: proper string

* stripped required

* chore: added strip to e2e tests

* chore: removed redundant space

* chore: removed space

* chore: style changes

* chore: fixed bugs

* chore: doc style

* chore: use variable

* chore: docs

* chore: grammar

* chore: doc grammer
  • Loading branch information
Rishab Luthra authored Jul 10, 2019
1 parent ae37235 commit cce5a65
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,21 @@ Returns a paged list of all permissions currently associated with this vehicle.
#### Return
| Type | Description |
|:------------------ |:--------------------|
| List[String] | An array of permissions. |
| String[] | An array of permissions. |

### `has_permissions(self, permissions)`

Returns whether the vehicle contains specified permission(s).

#### Arguments
| Parameter | Type | Description |
|:--------------- |:---|:------------- |
| `permissions` | String[] or String | The permission(s) to check. |

#### Return
| Type | Description |
|:------------------ |:--------------------|
| Boolean | Whether the vehicle has the specified permission(s). |

### `info(self)`

Expand Down
2 changes: 1 addition & 1 deletion smartcar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '3.0.3'
__version__ = '3.0.4'

from .smartcar import (AuthClient, is_expired, get_user_id, get_vehicle_ids)
from .vehicle import Vehicle
Expand Down
22 changes: 22 additions & 0 deletions smartcar/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ def permissions(self):
response = self.api.permissions()

return response.json()['permissions']

def has_permissions(self, permissions):
""" Checks if vehicle has specified permission(s).
Args:
permissions (str or list of str): Permission(s) to check
Returns:
boolean: Whether the vehicle has the specified permission(s)
"""
vehicle_permissions = self.permissions()
prefix = "required:"

if isinstance(permissions, list):
contained = [permission.replace(prefix, '', 1) in vehicle_permissions for permission in permissions]

if False in contained:
return False
else:
return True
else:
return permissions.replace(prefix, '', 1) in vehicle_permissions

def disconnect(self):
""" Disconnect this vehicle from the connected application.
Expand Down
19 changes: 19 additions & 0 deletions tests/test_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ def test_permission(self):

self.check(response)
self.assertEqual(response, data['permissions'])

@responses.activate
def test_has_permissions(self):
data = {
"permissions": ["read_odometer" , "read_vehicle_info"]
}

self.queue('GET', 'permissions', data)
single_response = self.vehicle.has_permissions("read_odometer")
required_response = self.vehicle.has_permissions("required:read_odometer")
multi_response = self.vehicle.has_permissions(["read_odometer", "required:read_vehicle_info"])
false_response = self.vehicle.has_permissions("read_location")
false_multi_response = self.vehicle.has_permissions(["read_odometer", "read_location"])

self.assertTrue(single_response)
self.assertTrue(required_response)
self.assertTrue(multi_response)
self.assertFalse(false_response)
self.assertFalse(false_multi_response)

@responses.activate
def test_info(self):
Expand Down
11 changes: 11 additions & 0 deletions tests/tests_e2e/test_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ def test_permissions(self):
permissions = self.vehicle.permissions()
self.assertIsNotNone(permissions)

def test_has_permissions(self):
single_response = self.vehicle.has_permissions("required:read_odometer")
multi_response = self.vehicle.has_permissions(["read_odometer", "required:read_vehicle_info"])
false_response = self.vehicle.has_permissions("read_ignition")
false_multi_response = self.vehicle.has_permissions(["read_odometer", "read_ignition"])

self.assertTrue(single_response)
self.assertTrue(multi_response)
self.assertFalse(false_response)
self.assertFalse(false_multi_response)

def test_vin(self):
vin = self.vehicle.vin()
self.assertIsNotNone(vin)
Expand Down

0 comments on commit cce5a65

Please sign in to comment.