forked from Vonage/vonage-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_voice.py
More file actions
151 lines (105 loc) · 4.76 KB
/
Copy pathtest_voice.py
File metadata and controls
151 lines (105 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os.path
import time
import jwt
import nexmo
from util import *
@responses.activate
def test_create_call(client, dummy_data):
stub(responses.POST, "https://api.nexmo.com/v1/calls")
params = {
"to": [{"type": "phone", "number": "14843331234"}],
"from": {"type": "phone", "number": "14843335555"},
"answer_url": ["https://example.com/answer"],
}
assert isinstance(client.create_call(params), dict)
assert request_user_agent() == dummy_data.user_agent
assert request_content_type() == "application/json"
@responses.activate
def test_get_calls(client, dummy_data):
stub(responses.GET, "https://api.nexmo.com/v1/calls")
assert isinstance(client.get_calls(), dict)
assert request_user_agent() == dummy_data.user_agent
assert_re(r"\ABearer ", request_authorization())
@responses.activate
def test_get_call(client, dummy_data):
stub(responses.GET, "https://api.nexmo.com/v1/calls/xx-xx-xx-xx")
assert isinstance(client.get_call("xx-xx-xx-xx"), dict)
assert request_user_agent() == dummy_data.user_agent
assert_re(r"\ABearer ", request_authorization())
@responses.activate
def test_update_call(client, dummy_data):
stub(responses.PUT, "https://api.nexmo.com/v1/calls/xx-xx-xx-xx")
assert isinstance(client.update_call("xx-xx-xx-xx", action="hangup"), dict)
assert request_user_agent() == dummy_data.user_agent
assert request_content_type() == "application/json"
assert request_body() == b'{"action": "hangup"}'
@responses.activate
def test_send_audio(client, dummy_data):
stub(responses.PUT, "https://api.nexmo.com/v1/calls/xx-xx-xx-xx/stream")
assert isinstance(
client.send_audio("xx-xx-xx-xx", stream_url="http://example.com/audio.mp3"),
dict,
)
assert request_user_agent() == dummy_data.user_agent
assert request_content_type() == "application/json"
assert request_body() == b'{"stream_url": "http://example.com/audio.mp3"}'
@responses.activate
def test_stop_audio(client, dummy_data):
stub(responses.DELETE, "https://api.nexmo.com/v1/calls/xx-xx-xx-xx/stream")
assert isinstance(client.stop_audio("xx-xx-xx-xx"), dict)
assert request_user_agent() == dummy_data.user_agent
@responses.activate
def test_send_speech(client, dummy_data):
stub(responses.PUT, "https://api.nexmo.com/v1/calls/xx-xx-xx-xx/talk")
assert isinstance(client.send_speech("xx-xx-xx-xx", text="Hello"), dict)
assert request_user_agent() == dummy_data.user_agent
assert request_content_type() == "application/json"
assert request_body() == b'{"text": "Hello"}'
@responses.activate
def test_stop_speech(client, dummy_data):
stub(responses.DELETE, "https://api.nexmo.com/v1/calls/xx-xx-xx-xx/talk")
assert isinstance(client.stop_speech("xx-xx-xx-xx"), dict)
assert request_user_agent() == dummy_data.user_agent
@responses.activate
def test_send_dtmf(client, dummy_data):
stub(responses.PUT, "https://api.nexmo.com/v1/calls/xx-xx-xx-xx/dtmf")
assert isinstance(client.send_dtmf("xx-xx-xx-xx", digits="1234"), dict)
assert request_user_agent() == dummy_data.user_agent
assert request_content_type() == "application/json"
assert request_body() == b'{"digits": "1234"}'
@responses.activate
def test_user_provided_authorization(client, dummy_data):
stub(responses.GET, "https://api.nexmo.com/v1/calls/xx-xx-xx-xx")
application_id = "different-nexmo-application-id"
nbf = int(time.time())
exp = nbf + 3600
client.auth(application_id=application_id, nbf=nbf, exp=exp)
client.get_call("xx-xx-xx-xx")
token = request_authorization().split()[1]
token = jwt.decode(token, dummy_data.public_key, algorithm="RS256")
assert token["application_id"] == application_id
assert token["nbf"] == nbf
assert token["exp"] == exp
@responses.activate
def test_authorization_with_private_key_path(dummy_data):
stub(responses.GET, "https://api.nexmo.com/v1/calls/xx-xx-xx-xx")
private_key = os.path.join(os.path.dirname(__file__), "data/private_key.txt")
client = nexmo.Client(
key=dummy_data.api_key,
secret=dummy_data.api_secret,
application_id=dummy_data.application_id,
private_key=private_key,
)
client.get_call("xx-xx-xx-xx")
token = jwt.decode(
request_authorization().split()[1], dummy_data.public_key, algorithm="RS256"
)
assert token["application_id"] == dummy_data.application_id
@responses.activate
def test_authorization_with_private_key_object(client, dummy_data):
stub(responses.GET, "https://api.nexmo.com/v1/calls/xx-xx-xx-xx")
client.get_call("xx-xx-xx-xx")
token = jwt.decode(
request_authorization().split()[1], dummy_data.public_key, algorithm="RS256"
)
assert token["application_id"] == dummy_data.application_id