1616from datetime import datetime
1717
1818import bandwidth
19+ from hamcrest import *
1920from bandwidth .api import messages_api
2021from bandwidth .model .list_message_direction_enum import ListMessageDirectionEnum
2122from bandwidth .model .list_message_item import ListMessageItem
2627from bandwidth .model .priority_enum import PriorityEnum
2728from bandwidth .model .message import Message
2829from bandwidth .exceptions import ApiException , UnauthorizedException
30+ from test .utils .env_variables import *
2931
3032
3133class TestMessagesApi (unittest .TestCase ):
@@ -34,21 +36,21 @@ class TestMessagesApi(unittest.TestCase):
3436 def setUp (self ):
3537 # API Client
3638 configuration = bandwidth .Configuration (
37- username = os . environ . get ( ' BW_USERNAME' ) ,
38- password = os . environ . get ( ' BW_PASSWORD' )
39+ username = BW_USERNAME ,
40+ password = BW_PASSWORD
3941 )
4042 api_client = bandwidth .ApiClient (configuration )
4143 self .api_instance = messages_api .MessagesApi (api_client )
42- self .account_id = os . environ . get ( ' BW_ACCOUNT_ID' )
44+ self .account_id = BW_ACCOUNT_ID
4345
4446 # Unauthorized API Client
4547 self .unauthorized_api_client = bandwidth .ApiClient ()
4648 self .unauthorized_api_instance = messages_api .MessagesApi (self .unauthorized_api_client )
4749
4850 # Message Properties
49- self .application_id = os . environ . get ( ' BW_MESSAGING_APPLICATION_ID' )
50- self .to_number = [os . environ . get ( ' USER_NUMBER' ) ]
51- self .from_number = os . environ . get ( ' BW_NUMBER' )
51+ self .application_id = BW_MESSAGING_APPLICATION_ID
52+ self .to_number = [USER_NUMBER ]
53+ self .from_number = BW_NUMBER
5254 self .text = 'python integration'
5355 self .media = ['https://cdn2.thecatapi.com/images/MTY3ODIyMQ.jpg' ]
5456 self .tag = 'python integration tag'
@@ -77,104 +79,82 @@ def setUp(self):
7779 def test_create_message (self ):
7880 response = self .api_instance .create_message (self .account_id , self .message_request , _return_http_data_only = False )
7981
80- self . assertEqual (response [1 ], 202 )
82+ assert_that (response [1 ], equal_to ( 202 ) )
8183
8284 api_response = response [0 ]
83- self .assertIsInstance (api_response , Message )
84- self .assertEqual (api_response .application_id ,self .application_id )
85- self .assertEqual (api_response .to , self .to_number )
86- self .assertEqual (api_response ._from , self .from_number )
87- self .assertEqual (api_response .owner , self .from_number )
88- self .assertEqual (api_response .text , self .text )
89- self .assertEqual (api_response .media , self .media )
90- self .assertEqual (api_response .tag , self .tag )
91- self .assertIsInstance (api_response .priority , PriorityEnum )
92- self .assertEqual (api_response .priority , self .priority )
93- self .assertEqual (api_response .segment_count , 1 )
94- self .assertTrue (datetime .fromisoformat (api_response .time [:- 1 ]))
95-
96-
97- def test_create_message_bad_request (self ):
98- with self .assertRaises (ApiException ) as context :
99- self .api_instance .create_message (self .account_id , self .invalid_message_request )
100-
101- self .assertEqual (context .exception .status , 400 )
102-
103- e = json .loads (context .exception .body )
104- self .assertEqual (e ['type' ], 'request-validation' )
105- self .assertIsInstance (e ['description' ], str )
106- self .assertIsInstance (e ['fieldErrors' ], list )
107-
108- field_error = e ['fieldErrors' ][0 ]
109- self .assertEqual (field_error ['fieldName' ], 'to' )
110- self .assertEqual (field_error ['description' ], "'+invalid' must be replaced with a valid E164 formatted telephone number" )
111-
85+ assert_that (api_response , instance_of (Message ))
86+ assert_that (api_response , has_properties (
87+ 'application_id' , self .application_id ,
88+ 'to' , self .to_number ,
89+ '_from' , self .from_number ,
90+ 'owner' , self .from_number ,
91+ 'text' , self .text ,
92+ 'media' , self .media ,
93+ 'tag' , self .tag ,
94+ 'priority' , instance_of (PriorityEnum ),
95+ 'priority' , self .priority ,
96+ 'segment_count' , 1 ,
97+ )
98+ )
99+ assert_that (datetime .fromisoformat (api_response .time [:- 1 ]), instance_of (datetime ))
112100
113- def test_create_message_unauthorized (self ):
114- with self .assertRaises (UnauthorizedException ) as context :
115- self .unauthorized_api_instance .create_message (self .account_id , self .invalid_message_request )
116-
117- self .assertEqual (context .exception .status , 401 )
118101
119- e = json .loads (context .exception .body )
120- self .assertEqual (e ['type' ], 'unauthorized' )
121- self .assertEqual (e ['description' ], 'Authentication Failed' )
102+ def test_create_message_bad_request (self ):
103+ assert_that (calling (self .api_instance .create_message ).with_args (
104+ self .account_id , self .invalid_message_request )), raises (ApiException )
105+
106+
107+ def test_create_message_unauthorized (self ):
108+ assert_that (calling (self .unauthorized_api_instance .create_message ).with_args (
109+ self .account_id , self .invalid_message_request )), raises (UnauthorizedException )
122110
123111
124112 @unittest .skip ('The SDK catches incorrect content-type before making the request and attempts to create an ApiException,\
125113 but the creation of the exception fails since there is no response body. This should probably create some\
126114 kind of Client Exception instead, since this is not an actual API Exception.' )
127115 def test_create_message_invalid_media (self ):
128- with self .assertRaises ( ApiException ) as context :
129- self .api_instance . create_message ( self . account_id , self .message_request , _content_type = 'application/xml' )
116+ assert_that ( calling ( self .api_instance . create_message ). with_args (
117+ self .account_id , self .message_request , _content_type = 'application/xml' )), raises ( ApiException )
130118
131119
132120 def test_list_messages (self ):
133121 message_direction = ListMessageDirectionEnum ("OUTBOUND" )
134122
135123 response = self .api_instance .list_messages (self .account_id , message_direction = message_direction , _return_http_data_only = False )
136124
137- self . assertEqual (response [1 ], 200 )
125+ assert_that (response [1 ], equal_to ( 200 ) )
138126
139127 api_response = response [0 ]
140- self .assertIsInstance (api_response , MessagesList )
141- self .assertGreater (api_response .total_count , 0 )
142- self .assertTrue (api_response .messages [0 ], ListMessageItem )
128+ assert_that (api_response , instance_of (MessagesList ))
129+ assert_that (api_response , has_properties (
130+ 'total_count' , greater_than (0 ),
131+ 'messages' , instance_of (list )
132+ ))
133+
134+ assert_that (api_response .messages [0 ], instance_of (ListMessageItem ))
143135
144136 message = api_response .messages [0 ]
145- self .assertEqual (message .account_id , self .account_id )
146- self .assertRegex (message .destination_tn , '^\\ +[1-9]\\ d{1,14}$' )
147- self .assertEqual (message .message_direction , ListMessageDirectionEnum ("OUTBOUND" ))
148- self .assertTrue (message .message_id )
149- self .assertIsInstance (message .message_status , MessageStatusEnum )
150- self .assertIsInstance (message .message_type , MessageTypeEnum )
151- self .assertTrue (datetime .fromisoformat (message .receive_time [:- 1 ]))
152- self .assertTrue (message .segment_count )
153- self .assertRegex (message .source_tn , '^\\ +[1-9]\\ d{1,14}$' )
137+ assert_that (message , has_properties (
138+ 'account_id' , self .account_id ,
139+ 'destination_tn' , matches_regexp ('^\\ +[1-9]\\ d{1,14}$' ),
140+ 'message_direction' , ListMessageDirectionEnum ("OUTBOUND" ),
141+ 'message_id' , matches_regexp ('^.+$' ),
142+ 'message_status' , instance_of (MessageStatusEnum ),
143+ 'message_type' , instance_of (MessageTypeEnum ),
144+ 'segment_count' , greater_than (0 ),
145+ 'source_tn' , matches_regexp ('^\\ +[1-9]\\ d{1,14}$' )
146+ ))
147+ assert_that (datetime .fromisoformat (message .receive_time [:- 1 ]), instance_of (datetime ))
154148
155-
156- def test_list_messages_bad_request (self ):
157-
158- with self .assertRaises (ApiException ) as context :
159- self .api_instance .list_messages (self .account_id )
160-
161- self .assertEqual (context .exception .status , 400 )
162149
163- e = json . loads ( context . exception . body )
164- self .assertEqual ( e [ 'type' ], 'bad-request' )
165- self .assertIsInstance ( e [ 'description' ], str )
150+ def test_list_messages_bad_request ( self ):
151+ assert_that ( calling ( self .api_instance . list_messages ). with_args (
152+ self .account_id ), raises ( ApiException ) )
166153
167154
168155 def test_list_messages_unauthorized (self ):
169-
170- with self .assertRaises (UnauthorizedException ) as context :
171- self .unauthorized_api_instance .list_messages (self .account_id )
172-
173- self .assertEqual (context .exception .status , 401 )
174-
175- e = json .loads (context .exception .body )
176- self .assertEqual (e ['type' ], 'unauthorized' )
177- self .assertEqual (e ['description' ], 'Your request could not be authenticated' )
156+ assert_that (calling (self .unauthorized_api_instance .list_messages ).with_args (
157+ self .account_id ), raises (UnauthorizedException ))
178158
179159
180160if __name__ == '__main__' :
0 commit comments