File: test_smstrade.py

package info (click to toggle)
python-smstrade 0.2.4-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 216 kB
  • sloc: python: 748; makefile: 10
file content (292 lines) | stat: -rw-r--r-- 10,852 bytes parent folder | download | duplicates (4)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# -*- python -*-
# -*- coding: utf-8 -*-

import six

if six.PY2:
    from ConfigParser import SafeConfigParser
else:
    from configparser import ConfigParser as SafeConfigParser

import argparse
import decimal
import httpretty
import os
import pytest
import smstrade
import tempfile
import time


@pytest.fixture()
def cleandir():
    newpath = tempfile.mkdtemp()
    os.chdir(newpath)


@pytest.fixture()
def api():
    testconfig = SafeConfigParser(defaults=smstrade.DEFAULTS)
    testconfig.add_section('smstrade')
    testconfig.set('smstrade', 'key', 'testkey')
    testconfig.set('smstrade', 'from', 'testsender')
    return smstrade.SMSTradeAPI(testconfig)


def test_SMSTradeError():
    exception = smstrade.SMSTradeError(u"a new test")
    assert str(exception) == 'a new test'


@pytest.mark.usefixtures('cleandir')
def test_SMSTradeAPI_init():
    smstrade.SMSTradeAPI()


class TestSMSTradeAPI(object):
    def test__gsm0338_length(self, api):
        assert api._gsm0338_length(u'a') == 1
        assert api._gsm0338_length(u'€') == 2
        assert api._gsm0338_length(u'a€') == 3
        with pytest.raises(smstrade.SMSTradeError):
            api._gsm0338_length(u'Ł')

    def test__check_normal_message(self, api):
        api._check_normal_message(160 * u'a')
        with pytest.raises(smstrade.SMSTradeError):
            api._check_normal_message(161 * u'a')
        api.concat = True
        api._check_normal_message(1530 * u'a')
        with pytest.raises(smstrade.SMSTradeError):
            api._check_normal_message(1531 * u'a')

    def test__encoding_normal_message(self, api):
        api.charset = 'ISO-8859-1'
        api._check_normal_message(160 * u'a')
        with pytest.raises(smstrade.SMSTradeError):
            api._check_normal_message(80 * u'€')
        api.charset = 'ISO-8859-15'
        api._check_normal_message(80 * u'€')
        with pytest.raises(smstrade.SMSTradeError):
            api._check_normal_message(160 * u'Ω')
        api.charset = 'UTF-8'
        api._check_normal_message(160 * u'Ω')

    def test__check_unicode_message(self, api):
        api._check_unicode_message(70 * u'€')
        with pytest.raises(smstrade.SMSTradeError):
            api._check_unicode_message(70 * six.unichr(0x120AE))
        with pytest.raises(smstrade.SMSTradeError):
            api._check_unicode_message(71 * u'€')

    def test__check_binary_message(self, api):
        api._check_binary_message(140 * 'a0')
        with pytest.raises(smstrade.SMSTradeError):
            api._check_binary_message(141 * 'a0')
        with pytest.raises(smstrade.SMSTradeError):
            api._check_binary_message(30 * 'r0')

    def test__check_voice_message(self, api):
        api._check_voice_message(160 * u'a')
        with pytest.raises(smstrade.SMSTradeError):
            api._check_voice_message(161 * u'a')

    def test__check_message(self, api):
        api._check_message(160 * u'a')
        api.messagetype = 'flash'
        api._check_message(160 * u'a')
        api.messagetype = 'unicode'
        api._check_message(70 * u'€')
        api.messagetype = 'binary'
        api._check_message(140 * 'a0')
        api.messagetype = 'voice'
        api._check_message(160 * u'a')
        api.messagetype = 'wrong'
        with pytest.raises(smstrade.SMSTradeError):
            api._check_message('does not matter')

    def test__handle_response_body(self, api):
        assert api._handle_response_body('100') == {
            'status': smstrade.STATUS_OK}
        assert api._handle_response_body(str(
            smstrade.STATUS_NOT_ENOUGH_BALANCE)) == {
            'status': smstrade.STATUS_NOT_ENOUGH_BALANCE}
        assert api._handle_response_body('100\n12345678') == {
            'status': smstrade.STATUS_OK}
        api.message_id = True
        assert api._handle_response_body('100\n12345678') == {
            'status': smstrade.STATUS_OK,
            'message_id': '12345678'}
        with pytest.raises(smstrade.SMSTradeError):
            api._handle_response_body('100')
        assert api._handle_response_body('100\n12345678\n0') == {
            'status': smstrade.STATUS_OK,
            'message_id': '12345678'}
        api.cost = True
        assert api._handle_response_body('100\n12345678\n0') == {
            'status': smstrade.STATUS_OK,
            'message_id': '12345678',
            'cost': decimal.Decimal('0')}
        api.message_id = False
        assert api._handle_response_body('100\n\n0.055') == {
            'status': smstrade.STATUS_OK,
            'cost': decimal.Decimal('0.055')}
        assert api._handle_response_body('100\n\n0.055\n1') == {
            'status': smstrade.STATUS_OK,
            'cost': decimal.Decimal('0.055')}
        api.count = True
        assert api._handle_response_body('100\n\n0.055\n1') == {
            'status': smstrade.STATUS_OK,
            'cost': decimal.Decimal('0.055'),
            'count': 1}
        api.message_id = True
        assert api._handle_response_body('100\n12345678\n0.055\n1') == {
            'status': smstrade.STATUS_OK,
            'message_id': '12345678',
            'message_id': '12345678',
            'cost': decimal.Decimal('0.055'),
            'count': 1}

    @pytest.mark.usefixtures('cleandir')
    def test__add_optional_flags(self, api):
        for attrname, param in (
                ('debug', 'debug'),
                ('cost', 'cost'),
                ('message_id', 'message_id'),
                ('count', 'count'),
                ('reports', 'dlr'),
                ('response', 'response')):
            setattr(api, attrname, True)
            request_params = {}
            request_params = api._add_optional_flags(request_params)
            assert request_params[param] == 1
        api.route = smstrade.ROUTE_GOLD
        api.response = True
        request_params = {}
        api._add_optional_flags(request_params)
        assert 'response' not in request_params

    @pytest.mark.usefixtures('cleandir')
    def test__add_optional_fields(self, api):
        for attrname, param, value in (
                ('reference', 'ref', 'myref0815'),
                ('senddate', 'senddate', int(time.time() + 100)),
                ('messagetype', 'messagetype', 'flash')):
            setattr(api, attrname, value)
            request_params = {}
            request_params = api._add_optional_fields(request_params)
            assert request_params[param] == value

    @pytest.mark.usefixtures('cleandir')
    def test__build_request_parameters(self, api):
        api.key = 'Testkey'
        assert api._build_request_parameters('01717654321') == {
            'key': 'Testkey',
            'to': '01717654321',
            'route': 'basic',
            'debug': 1}
        api.route = smstrade.ROUTE_GOLD
        api.charset = 'UTF-8'
        api.sender = u'Dönermann'
        assert api._build_request_parameters('01717654321') == {
            'key': 'Testkey',
            'to': '01717654321',
            'route': 'gold',
            'debug': 1,
            'from': u'Dönermann'.encode('UTF-8'),
            'charset': 'UTF-8'}

    def test__send_normal_message(self, api):
        httpretty.enable()
        httpretty.register_uri(httpretty.POST, api.url, body='100')
        assert api._send_message('01717654321', 'Test') == {
            'status': smstrade.STATUS_OK}
        httpretty.register_uri(httpretty.POST, api.url,
                               body='100\n\n\n4')
        api.concat = True
        api.count = True
        assert api._send_message('01717654321', 140 * 'Test') == {
            'status': smstrade.STATUS_OK,
            'count': 4}

    def test__send_binary_message(self, api):
        httpretty.enable()
        httpretty.register_uri(httpretty.POST, api.url, body='100')
        api.messagetype = smstrade.MESSAGE_TYPE_BINARY
        api.udh = '040b02000820de'
        assert api._send_message('01717654321', u'48656c6c6f') == {
            'status': smstrade.STATUS_OK}

    def test__send_message(self, api):
        httpretty.enable()
        httpretty.register_uri(httpretty.POST, api.url, body='100')
        assert api._send_message('01717654321', u'Test') == {
            'status': smstrade.STATUS_OK}
        for messagetype, message in (
                (smstrade.MESSAGE_TYPE_FLASH, u'Test'),
                (smstrade.MESSAGE_TYPE_UNICODE, u'Привет мир'),
                (smstrade.MESSAGE_TYPE_BINARY, u'48656c6c6f'),
                (smstrade.MESSAGE_TYPE_VOICE, u'Test')):
            api.messagetype = messagetype
            assert api._send_message('01717654321', message) == {
                'status': smstrade.STATUS_OK}
        with pytest.raises(smstrade.SMSTradeError):
            api.messagetype = 'invalid'
            api._send_message('01717654321', u'Test')

    def test_send_sms(self, api):
        httpretty.enable()
        httpretty.register_uri(httpretty.POST, api.url, body='100')
        testargs = {'wrong': 'does not matter', 'from': 'test'}
        result = api.send_sms(['00491717654321'], 'Test', **testargs)
        assert result == {'00491717654321': {'status': smstrade.STATUS_OK}}


@pytest.mark.usefixtures('cleandir')
def test_empty_conffile():
    with open('dummy.ini', 'w') as inifile:
        inifile.write('')
    conf = SafeConfigParser()
    conf.read(['dummy.ini'])
    smstrade.SMSTradeAPI(conf)
    smstrade.SMSTradeBalanceAPI(conf)


def test_hexstr():
    assert smstrade.hexstr('A0a0') == 'A0a0'
    with pytest.raises(argparse.ArgumentTypeError):
        smstrade.hexstr('R0')


def test_secondssinceepoch():
    timestamp = str(int(time.time() + 100))
    assert smstrade.secondssinceepoch(timestamp) == int(timestamp)
    timestamp = str(int(time.time() - 100))
    with pytest.raises(argparse.ArgumentTypeError):
        smstrade.secondssinceepoch(timestamp)


@pytest.mark.usefixtures('cleandir')
def test_send_sms():
    httpretty.enable()
    httpretty.register_uri(httpretty.POST, smstrade.DEFAULTS['url'],
                           body='100')
    with pytest.raises(SystemExit):
        smstrade.send_sms([])
    smstrade.send_sms(['00491717654321', 'Test'])
    smstrade.send_sms(['--messagetype', 'binary', '00491717654321', 'Test'])
    with open('dummy.ini', 'w') as inifile:
        inifile.write('[smstrade]\nkey = fake')
    smstrade.send_sms(['--config', 'dummy.ini', '00491717654321', 'Test'])


@pytest.mark.usefixtures('cleandir')
def test_account_balance():
    httpretty.enable()
    httpretty.register_uri(
        httpretty.GET, smstrade.DEFAULTS['balanceurl'], body='0.000')
    smstrade.account_balance([])
    smstrade.account_balance(['--key', 'fake'])
    with open('dummy.ini', 'w') as inifile:
        inifile.write('[smstrade]\nkey = fake')
    smstrade.account_balance(['--config', 'dummy.ini'])