8000 Keys auth · PNPtutorials/twilio-python@6f6938f · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f6938f

Browse files
committed
Keys auth
1 parent 6bf0e94 commit 6f6938f

File tree

7 files changed

+180
-0
lines changed

7 files changed

+180
-0
lines changed

tests/resources/keys_instance.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"sid":"SKaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3+
"friendly_name":"Fuzzy Lumpkins' SigningKey",
4+
"date_created":"Fri, 13 Mar 2015 13:24:01 +0000",
5+
"date_updated":"Fri, 13 Mar 2015 13:24:01 +0000"
6+
}

tests/resources/keys_list.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"keys":[
3+
{
4+
"sid":"SK932e398ac43ca670b1609b05ee301e8c",
5+
"friendly_name":"Fuzzy Lumpkins' SigningKey",
6+
"date_created":"Fri, 13 Mar 2015 13:24:01 +0000",
7+
"date_updated":"Fri, 13 Mar 2015 13:24:01 +0000"
8+
}
9+
],
10+
"first_page_uri":"/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Keys.json?PageSize=50&Page=0",
11+
"uri":"/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Keys.json?PageSize=50&Page=0",
12+
"next_page_uri":null,
13+
"previous_page_uri":null,
14+
"page":0,
15+
"page_size":50,
16+
"start":0,
17+
"end":1
18+
}

tests/test_keys.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from mock import patch, Mock
2+
from twilio.rest.resources.keys import Keys, Key
3+
from tests.tools import create_mock_json
4+
5+
ACCOUNT_SID = "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6+
AUTH = (ACCOUNT_SID, "token")
7+
BASE_URL = "https://api.twilio.com/2010-04-01/Accounts/{}".format(ACCOUNT_SID)
8+
KEY_SID = "SKaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9+
10+
list_resource = Keys(BASE_URL, AUTH)
11+
12+
13+
@patch("twilio.rest.resources.base.make_twilio_request")
14+
def test_get_key(mock):
15+
resp = create_mock_json("tests/resources/keys_instance.json")
16+
mock.return_value = resp
17+
18+
url = BASE_URL + "/Keys/{}".format(KEY_SID)
19+
list_resource.get(KEY_SID)
20+
21+
mock.assert_called_with("GET", url, auth=AUTH, use_json_extension=True)
22+
23+
24+
@patch("twilio.rest.resources.base.make_twilio_request")
25+
def test_create_key(mock):
26+
resp = create_mock_json("tests/resources/keys_instance.json")
27+
resp.status_code = 201
28+
mock.return_value = resp
29+
30+
url = BASE_URL + "/Keys"
31+
list_resource.create(friendly_name="Fuzzy Lumpkins' SigningKey")
32+
params = {
33+
'FriendlyName': "Fuzzy Lumpkins' SigningKey"
34+
}
35+
36+
mock.assert_called_with("POST", url, data=params, auth=AUTH, use_json_extension=True)
37+
38+
39+
@patch("twilio.rest.resources.base.make_twilio_request")
40+
def test_update_key(mock):
41+
resp = create_mock_json("tests/resources/keys_instance.json")
42+
mock.return_value = resp
43+
44+
url = BASE_URL + "/Keys/{}".format(KEY_SID)
45+
list_resource.update(sid=KEY_SID, friendly_name="Fuzzy Lumpkins' SigningKey")
46+
params = {
47+
'FriendlyName': "Fuzzy Lumpkins' SigningKey"
48+
}
49+
50+
mock.assert_called_with("POST", url, data=params, auth=AUTH, use_json_extension=True)
51+
52+
53+
@patch("twilio.rest.resources.base.Resource.request")
54+
def test_delete_key(mock):
55+
resp = Mock()
56+
resp.content = ""
57+
resp.status_code = 204
58+
mock.return_value = resp, {}
59+
60+
key = Key(list_resource, KEY_SID)
61+
key.delete()
62+
63+
url = BASE_URL + "/Keys/{}".format(KEY_SID)
64+
mock.assert_called_with("DELETE", url)
65+
66+
67+
@patch("twilio.rest.resources.base.make_twilio_request")
68+
def test_list_keys(mock):
69+
resp = create_mock_json("tests/resources/keys_list.json")
70+
mock.return_value = resp
71+
72+
url = BASE_URL + "/Keys"
73+
list_resource.list()
74+
75+
mock.assert_called_with("GET", url, params={}, auth=AUTH, use_json_extension=True)

twilio/rest/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Conferences,
1212
ConnectApps,
1313
DependentPhoneNumbers,
14+
Keys,
1415
MediaList,
1516
Members,
1617
Messages,
@@ -76,6 +77,7 @@ def __init__(self, account=None, token=None, base="https://api.twilio.com",
7677
self.sip = Sip(self.account_uri, self.auth, timeout)
7778
self.signing_keys = SigningKeys(self.account_uri, self.auth, timeout)
7879
self.tokens = Tokens(self.account_uri, self.auth, timeout)
80+
self.keys = Keys(self.account_uri, self.auth, timeout)
7981

8082
def participants(self, conference_sid):
8183
"""

twilio/rest/resources/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,5 @@
7575
DependentPhoneNumber,
7676
DependentPhoneNumbers,
7777
)
78+
79+
from .keys import Key, Keys

twilio/rest/resources/accounts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .conferences import Conferences
1010
from .connect_apps import ConnectApps, AuthorizedConnectApps
1111
from .queues import Queues
12+
from twilio.rest.resources.keys import Keys
1213
from .usage import UsageRecords, UsageTriggers
1314
from .messages import Messages
1415
from .media import MediaList
@@ -44,6 +45,7 @@ class Account(InstanceResource):
4445
Messages,
4546
SigningKeys,
4647
Sip,
48+
Keys,
4749
]
4850

4951
def update(self, **kwargs):

twilio/rest/resources/keys.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from twilio.rest.resources.base import InstanceResource, ListResource
2+
3+
4+
class Key(InstanceResource):
5+
"""
6+
A key resource.
7+
See https://www.twilio.com/docs/api/rest-keys
8+
9+
.. attribute:: sid
10+
11+
The unique ID for this key.
12+
13+
.. attribute:: friendly_name
14+
15+
A human-readable description of this key.
16+
17+
.. attribute:: secret
18+
19+
This key's secret.
20+
21+
.. attribute:: date_created
22+
23+
The date this key was created, given as UTC in ISO 8601 format.
24+
25+
.. attribute:: date_updated
26+
27+
The date this singing key was last updated, given as UTC in ISO 8601
28+
format.
29+
"""
30+
31+
def update(self, **kwargs):
32+
"""
33+
Update this key
34+
"""
35+
return self.parent.update(self.name, **kwargs)
36+
37+
def delete(self):
38+
"""
39+
Delete this key
40+
"""
41+
return self.parent.delete(self.name)
42+
43+
44+
class Keys(ListResource):
45+
name = "Keys"
46+
key = "keys"
47+
instance = Key
48+
49+
def create(self, **kwargs):
50+
"""
51+
Create a :class:`Key` with any of these optional parameters.
52+
53+
:param friendly_name: A human readable description of the signing key.
54+
"""
55+
return self.create_instance(kwargs)
56+
57+
def update(self, sid, **kwargs):
58+
"""
59+
Update a :class:`Key` with the given parameters.
60+
61+
All the parameters are describe above in :meth:`create`
62+
"""
63+
return self.update_instance(sid, kwargs)
64+
65+
def delete(self, sid):
66+
"""
67+
Delete a :class:`Key`
68+
"""
69+
return self.delete_instance(sid)
70+
71+
def list(self, **kwargs):
72+
"""
73+
Returns a page of :class:`Key` resources as a list
74+
"""
75+
return self.get_instances(kwargs)

0 commit comments

Comments
 (0)
0