|
2 | 2 |
|
3 | 3 | import httpretty |
4 | 4 | import intercom |
| 5 | +import json |
5 | 6 | import re |
6 | 7 | from describe import expect |
7 | 8 | from intercom import Intercom |
| 9 | +from intercom import UnexpectedError |
8 | 10 |
|
9 | 11 | get = httpretty.GET |
10 | 12 | post = httpretty.POST |
@@ -54,3 +56,134 @@ def it_raises_service_unavailable_error(self): |
54 | 56 | get, r(r'/notes$'), body='', status=503) |
55 | 57 | with expect.to_raise_error(intercom.ServiceUnavailableError): |
56 | 58 | Intercom.get('/notes') |
| 59 | + |
| 60 | + @httpretty.activate |
| 61 | + def it_raises_an_unexpected_typed_error(self): |
| 62 | + payload = { |
| 63 | + 'type': 'error.list', |
| 64 | + 'errors': [ |
| 65 | + { |
| 66 | + 'type': 'hopper', |
| 67 | + 'message': 'The first compiler.' |
| 68 | + } |
| 69 | + ] |
| 70 | + } |
| 71 | + httpretty.register_uri(get, r("/users"), body=json.dumps(payload)) |
| 72 | + try: |
| 73 | + Intercom.get('/users') |
| 74 | + except (UnexpectedError) as err: |
| 75 | + assert "The error of type 'hopper' is not recognized" in err.message # noqa |
| 76 | + expect(err.context['http_code']) == 200 |
| 77 | + expect(err.context['application_error_code']) == 'hopper' |
| 78 | + |
| 79 | + @httpretty.activate |
| 80 | + def it_raises_an_unexpected_untyped_error(self): |
| 81 | + payload = { |
| 82 | + 'type': 'error.list', |
| 83 | + 'errors': [ |
| 84 | + { |
| 85 | + 'message': 'UNIVAC' |
| 86 | + } |
| 87 | + ] |
| 88 | + } |
| 89 | + httpretty.register_uri(get, r("/users"), body=json.dumps(payload)) |
| 90 | + try: |
| 91 | + Intercom.get('/users') |
| 92 | + except (UnexpectedError) as err: |
| 93 | + assert "An unexpected error occured." in err.message |
| 94 | + expect(err.context['application_error_code']) is None |
| 95 | + |
| 96 | + @httpretty.activate |
| 97 | + def it_raises_a_bad_request_error(self): |
| 98 | + payload = { |
| 99 | + 'type': 'error.list', |
| 100 | + 'errors': [ |
| 101 | + { |
| 102 | + 'type': None, |
| 103 | + 'message': 'email is required' |
| 104 | + } |
| 105 | + ] |
| 106 | + } |
| 107 | + |
| 108 | + for code in ['missing_parameter', 'parameter_invalid', 'bad_request']: |
| 109 | + payload['errors'][0]['type'] = code |
| 110 | + httpretty.register_uri(get, r("/users"), body=json.dumps(payload)) |
| 111 | + with expect.to_raise_error(intercom.BadRequestError): |
| 112 | + Intercom.get('/users') |
| 113 | + |
| 114 | + @httpretty.activate |
| 115 | + def it_raises_an_authentication_error(self): |
| 116 | + payload = { |
| 117 | + 'type': 'error.list', |
| 118 | + 'errors': [ |
| 119 | + { |
| 120 | + 'type': 'unauthorized', |
| 121 | + 'message': 'Your name\'s not down.' |
| 122 | + } |
| 123 | + ] |
| 124 | + } |
| 125 | + for code in ['unauthorized', 'forbidden']: |
| 126 | + payload['errors'][0]['type'] = code |
| 127 | + httpretty.register_uri(get, r("/users"), body=json.dumps(payload)) |
| 128 | + with expect.to_raise_error(intercom.AuthenticationError): |
| 129 | + Intercom.get('/users') |
| 130 | + |
| 131 | + @httpretty.activate |
| 132 | + def it_raises_resource_not_found_by_type(self): |
| 133 | + payload = { |
| 134 | + 'type': 'error.list', |
| 135 | + 'errors': [ |
| 136 | + { |
| 137 | + 'type': 'not_found', |
| 138 | + 'message': 'Waaaaally?' |
| 139 | + } |
| 140 | + ] |
| 141 | + } |
| 142 | + httpretty.register_uri(get, r("/users"), body=json.dumps(payload)) |
| 143 | + with expect.to_raise_error(intercom.ResourceNotFound): |
| 144 | + Intercom.get('/users') |
| 145 | + |
| 146 | + @httpretty.activate |
| 147 | + def it_raises_rate_limit_exceeded(self): |
| 148 | + payload = { |
| 149 | + 'type': 'error.list', |
| 150 | + 'errors': [ |
| 151 | + { |
| 152 | + 'type': 'rate_limit_exceeded', |
| 153 | + 'message': 'Fair use please.' |
| 154 | + } |
| 155 | + ] |
| 156 | + } |
| 157 | + httpretty.register_uri(get, r("/users"), body=json.dumps(payload)) |
| 158 | + with expect.to_raise_error(intercom.RateLimitExceeded): |
| 159 | + Intercom.get('/users') |
| 160 | + |
| 161 | + @httpretty.activate |
| 162 | + def it_raises_a_service_unavailable_error(self): |
| 163 | + payload = { |
| 164 | + 'type': 'error.list', |
| 165 | + 'errors': [ |
| 166 | + { |
| 167 | + 'type': 'service_unavailable', |
| 168 | + 'message': 'Zzzzz.' |
| 169 | + } |
| 170 | + ] |
| 171 | + } |
| 172 | + httpretty.register_uri(get, r("/users"), body=json.dumps(payload)) |
| 173 | + with expect.to_raise_error(intercom.ServiceUnavailableError): |
| 174 | + Intercom.get('/users') |
| 175 | + |
| 176 | + @httpretty.activate |
| 177 | + def it_raises_a_multiple_matching_users_error(self): |
| 178 | + payload = { |
| 179 | + 'type': 'error.list', |
| 180 | + 'errors': [ |
| 181 | + { |
| 182 | + 'type': 'conflict', |
| 183 | + 'message': 'Two many cooks.' |
| 184 | + } |
| 185 | + ] |
| 186 | + } |
| 187 | + httpretty.register_uri(get, r("/users"), body=json.dumps(payload)) |
| 188 | + with expect.to_raise_error(intercom.MultipleMatchingUsersError): |
| 189 | + Intercom.get('/users') |
0 commit comments