def test_reauthentication_on_session_timeout(self): """ If the session_id property is called when the session has actually timed out it should need to reauthenticate and get new a new session_id """ clickatell = Clickatell("username", "password", "api_id", \ client_class=TestClient, sendmsg_defaults=sendmsg_defaults) # manually set it for comparison later clickatell.session_id = "old_session_hash" self.assertEquals(clickatell.session_id, 'old_session_hash') # manually expire by setting the session_start_time beyond the allowed # timeout clickatell._session_start_time = datetime.now() - \ (Clickatell.SESSION_TIME_OUT * 2) # next api.session_id call should call the auth url # because the session's timed out clickatell.client.mock('GET', auth_url, { 'user': '******', 'password': '******', 'api_id': 'api_id' }, response=clickatell.client.parse_content('OK: new_session_hash')) self.assertEquals(clickatell.session_id, 'new_session_hash') # make sure the URLs we expected to be called all actually did self.assertTrue(clickatell.client.all_mocks_called())
def _setup_clickatell_for_batch_test(self): clickatell = Clickatell('username', 'password', 'api_id', client_class=TestClient, sendmsg_defaults=sendmsg_defaults) clickatell.session_id = 'session_id' client = clickatell.client # it should start the batch client.mock('GET', batch_start_url, merge_with_defaults({ 'session_id': clickatell.session_id, 'template': 'Hello #field1# #field2#', 'from': '27123456789' }), response=client.parse_content('ID: batch_id')) # it should send two messages via the batch client.mock('GET', batch_send_url, { 'session_id': clickatell.session_id, 'batch_id': 'batch_id', 'to': '27123456781', 'field1': 'Foo 1', 'field2': 'Bar 1' }, response=client.parse_content('ID: apimsgid1')) client.mock('GET', batch_send_url, { 'session_id': clickatell.session_id, 'batch_id': 'batch_id', 'to': '27123456782', 'field1': 'Foo 2', 'field2': 'Bar 2' }, response=client.parse_content('ID: apimsgid2')) # it should end the batch client.mock('GET', batch_end_url, { 'session_id': clickatell.session_id, 'batch_id': 'batch_id' }, response=client.parse_content('OK')) return clickatell
def test_getbalance_fail(self): clickatell = Clickatell('username', 'password', 'api_id', client_class=TestClient) clickatell.session_id = 'session_id' clickatell.client.mock('GET', getbalance_url, { 'session_id': 'session_id' }, response=clickatell.client.parse_content('ERR: 003, Session ID Expired')) self.assertRaises(ClickatellError, clickatell.getbalance)
def test_getbalance(self): clickatell = Clickatell('username', 'password', 'api_id', client_class=TestClient) clickatell.session_id = "session_id" self.assertFalse(clickatell.session_expired()) clickatell.client.mock('GET', getbalance_url, { 'session_id': 'session_id' }, response=clickatell.client.parse_content('Credit: 500.00')) self.assertEquals(clickatell.getbalance(), 500.00)
def test_sendmsg_recipient_validation(self): clickatell = Clickatell('username','password','api_id', client_class=TestClient, sendmsg_defaults=sendmsg_defaults) clickatell.session_id = "session_id" self.assertFalse(clickatell.session_expired()) for recipient in ['+27123456781', '0027123456781']: kwargs = { 'sender': '27123456781', 'recipients': [recipient], 'text': 'hello world' } self.assertRaises(ClickatellError, clickatell.sendmsg, **kwargs)
def test_getmsgcharge_fail(self): clickatell = Clickatell('username', 'password', 'api_id', client_class=TestClient) clickatell.session_id = "session_id" clickatell.client.mock('GET', getmsgcharge_url, { 'session_id': clickatell.session_id, 'apimsgid': 'apimsgid' }, response=clickatell.client.parse_content( 'ERR: 108, Invalid or missing API ID' )) resp = clickatell.getmsgcharge(apimsgid='apimsgid') self.assertTrue(isinstance(resp, ERRResponse)) self.assertEquals(resp.code, 108) self.assertEquals(resp.reason, 'Invalid or missing API ID')
def test_getmsgcharge(self): clickatell = Clickatell('username', 'password', 'api_id', client_class=TestClient) clickatell.session_id = "session_id" clickatell.client.mock('GET', getmsgcharge_url, { 'session_id': clickatell.session_id, 'apimsgid': 'apimsgid' }, response=clickatell.client.parse_content( 'apiMsgId: apimsgid charge: 1 status: 002' )) resp = clickatell.getmsgcharge(apimsgid='apimsgid') self.assertTrue(isinstance(resp, ApiMsgIdResponse)) self.assertEquals(resp.value, 'apimsgid') self.assertEquals(resp.extra, {'charge': '1', 'status': '002'})
def test_check_coverage_fail(self): clickatell = Clickatell('username', 'password', 'api_id', client_class=TestClient) clickatell.session_id = "session_id" clickatell.client.mock('GET', check_coverage_url, { 'session_id': clickatell.session_id, 'msisdn': '27123456781' }, response=clickatell.client.parse_content( 'ERR: This prefix is not currently supported. Messages sent to '\ 'this prefix will fail. Please contact support for assistance.' )) resp = clickatell.check_coverage(msisdn='27123456781') self.assertTrue(isinstance(resp, ERRResponse)) self.assertEquals(resp.value, 'This prefix is not currently supported. Messages sent to this '\ 'prefix will fail. Please contact support for assistance.')
def test_check_coverage(self): clickatell = Clickatell('username', 'password', 'api_id', client_class=TestClient) clickatell.session_id = "session_id" clickatell.client.mock('GET', check_coverage_url, { 'session_id': clickatell.session_id, 'msisdn': '27123456781' }, response=clickatell.client.parse_content( 'OK: This prefix is currently supported. Messages sent to ' \ 'this prefix will be routed. Charge: 1' )) resp = clickatell.check_coverage(msisdn='27123456781') self.assertTrue(isinstance(resp, OKResponse)) self.assertEquals(resp.value, 'This prefix is currently supported. '\ 'Messages sent to this prefix will '\ 'be routed.') self.assertEquals(resp.extra['Charge'], '1')
def test_quicksend(self): clickatell = Clickatell('username', 'password', 'api_id', client_class=TestClient, sendmsg_defaults=sendmsg_defaults) clickatell.session_id = 'session_id' client = clickatell.client # it should start the batch client.mock('GET', batch_start_url, merge_with_defaults({ 'session_id': clickatell.session_id, 'template': 'Hello world!', 'from': '27123456789' }), response=client.parse_content('ID: batch_id')) # it should send two messages via the batch client.mock('GET', batch_quicksend_url, { 'session_id': clickatell.session_id, 'batch_id': 'batch_id', 'to': '27123456781,27123456782,27123456783' }, response=client.parse_content('ID: apimsgid1\n' \ 'ID: apimsgid2\n' \ 'ID: apimsgid3\n')) # it should end the batch client.mock('GET', batch_end_url, { 'session_id': clickatell.session_id, 'batch_id': 'batch_id' }, response=client.parse_content('OK')) with clickatell.batch(sender='27123456789', template='Hello world!') as batch: [apimsgid1, apimsgid2, apimsgid3] = batch.quicksend(recipients=[ '27123456781', '27123456782', '27123456783', ]) self.assertEquals(apimsgid1.value, 'apimsgid1') self.assertEquals(apimsgid2.value, 'apimsgid2') self.assertEquals(apimsgid3.value, 'apimsgid3') self.assertTrue(clickatell.client.all_mocks_called())