|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:mockito/annotations.dart'; |
| 5 | +import 'package:mockito/mockito.dart'; |
| 6 | +import 'package:stackwallet/networking/http.dart'; |
| 7 | +import 'package:stackwallet/utilities/paynym_is_api.dart'; |
| 8 | + |
| 9 | +import 'paynym_is_api_test.mocks.dart'; |
| 10 | + |
| 11 | +@GenerateMocks([HTTP]) |
| 12 | +void main() { |
| 13 | + late PaynymIsApi api; |
| 14 | + late MockHTTP client; |
| 15 | + |
| 16 | + setUp(() { |
| 17 | + client = MockHTTP(); |
| 18 | + api = PaynymIsApi(); |
| 19 | + api.client = client; |
| 20 | + }); |
| 21 | + |
| 22 | + void stubPost(String endpoint, String responseBody, int statusCode, |
| 23 | + {Map<String, String>? extraHeaders}) { |
| 24 | + when(client.post( |
| 25 | + url: Uri.parse('https://paynym.rs/api/v1$endpoint'), |
| 26 | + headers: anyNamed('headers'), |
| 27 | + proxyInfo: anyNamed('proxyInfo'), |
| 28 | + body: anyNamed('body'), |
| 29 | + encoding: anyNamed('encoding'), |
| 30 | + )).thenAnswer((_) async => |
| 31 | + Response(utf8.encode(responseBody), statusCode)); |
| 32 | + } |
| 33 | + |
| 34 | + group('create', () { |
| 35 | + test('400 with empty body returns typed error', () async { |
| 36 | + stubPost('/create', '', 400); |
| 37 | + final r = await api.create('PM8Ttest'); |
| 38 | + expect(r.statusCode, 400); |
| 39 | + expect(r.message, 'Bad request'); |
| 40 | + expect(r.value, isNull); |
| 41 | + }); |
| 42 | + |
| 43 | + test('201 with valid JSON returns CreatedPaynym', () async { |
| 44 | + stubPost('/create', |
| 45 | + '{"claimed":false,"nymID":"abc","nymName":"foo","segwit":true,"token":"tok"}', |
| 46 | + 201); |
| 47 | + final r = await api.create('PM8Ttest'); |
| 48 | + expect(r.statusCode, 201); |
| 49 | + expect(r.message, 'PayNym created successfully'); |
| 50 | + expect(r.value, isNotNull); |
| 51 | + expect(r.value!.nymId, 'abc'); |
| 52 | + }); |
| 53 | + |
| 54 | + test('200 returns existing PayNym', () async { |
| 55 | + stubPost('/create', |
| 56 | + '{"claimed":true,"nymID":"abc","nymName":"foo","segwit":true,"token":"tok"}', |
| 57 | + 200); |
| 58 | + final r = await api.create('PM8Ttest'); |
| 59 | + expect(r.statusCode, 200); |
| 60 | + expect(r.message, 'PayNym already exists'); |
| 61 | + expect(r.value, isNotNull); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + group('token', () { |
| 66 | + test('404 with empty body returns typed error', () async { |
| 67 | + stubPost('/token', '', 404); |
| 68 | + final r = await api.token('PM8Ttest'); |
| 69 | + expect(r.statusCode, 404); |
| 70 | + expect(r.message, 'Payment code was not found'); |
| 71 | + expect(r.value, isNull); |
| 72 | + }); |
| 73 | + |
| 74 | + test('400 with empty body returns typed error', () async { |
| 75 | + stubPost('/token', '', 400); |
| 76 | + final r = await api.token('PM8Ttest'); |
| 77 | + expect(r.statusCode, 400); |
| 78 | + expect(r.message, 'Bad request'); |
| 79 | + expect(r.value, isNull); |
| 80 | + }); |
| 81 | + |
| 82 | + test('200 with valid JSON returns token string', () async { |
| 83 | + stubPost('/token', '{"token":"testToken123"}', 200); |
| 84 | + final r = await api.token('PM8Ttest'); |
| 85 | + expect(r.statusCode, 200); |
| 86 | + expect(r.message, 'Token was successfully updated'); |
| 87 | + expect(r.value, 'testToken123'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + group('nym', () { |
| 92 | + test('404 with empty body returns typed error', () async { |
| 93 | + stubPost('/nym', '', 404); |
| 94 | + final r = await api.nym('PM8Ttest'); |
| 95 | + expect(r.statusCode, 404); |
| 96 | + expect(r.message, 'Nym not found'); |
| 97 | + expect(r.value, isNull); |
| 98 | + }); |
| 99 | + |
| 100 | + test('400 with empty body returns typed error', () async { |
| 101 | + stubPost('/nym', '', 400); |
| 102 | + final r = await api.nym('PM8Ttest'); |
| 103 | + expect(r.statusCode, 400); |
| 104 | + expect(r.message, 'Bad request'); |
| 105 | + expect(r.value, isNull); |
| 106 | + }); |
| 107 | + |
| 108 | + test('200 with valid JSON returns PaynymAccount', () async { |
| 109 | + stubPost('/nym', jsonEncode({ |
| 110 | + 'nymID': 'testId', |
| 111 | + 'nymName': 'testName', |
| 112 | + 'segwit': true, |
| 113 | + 'codes': [ |
| 114 | + {'claimed': true, 'segwit': true, 'code': 'PM8Ttest'} |
| 115 | + ], |
| 116 | + 'followers': <Map<String, dynamic>>[], |
| 117 | + 'following': <Map<String, dynamic>>[], |
| 118 | + }), 200); |
| 119 | + final r = await api.nym('PM8Ttest'); |
| 120 | + expect(r.statusCode, 200); |
| 121 | + expect(r.message, 'Nym found and returned'); |
| 122 | + expect(r.value, isNotNull); |
| 123 | + expect(r.value!.nymID, 'testId'); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + group('claim', () { |
| 128 | + test('400 with empty body returns typed error', () async { |
| 129 | + stubPost('/claim', '', 400); |
| 130 | + final r = await api.claim('tok', 'sig'); |
| 131 | + expect(r.statusCode, 400); |
| 132 | + expect(r.message, 'Bad request'); |
| 133 | + expect(r.value, isNull); |
| 134 | + }); |
| 135 | + |
| 136 | + test('200 with valid JSON returns PaynymClaim', () async { |
| 137 | + stubPost('/claim', |
| 138 | + '{"claimed":"PM8Ttest","token":"newTok"}', 200); |
| 139 | + final r = await api.claim('tok', 'sig'); |
| 140 | + expect(r.statusCode, 200); |
| 141 | + expect(r.message, 'Payment code successfully claimed'); |
| 142 | + expect(r.value, isNotNull); |
| 143 | + expect(r.value!.claimed, 'PM8Ttest'); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + group('follow', () { |
| 148 | + test('404 with empty body returns typed error', () async { |
| 149 | + stubPost('/follow', '', 404); |
| 150 | + final r = await api.follow('tok', 'sig', 'target'); |
| 151 | + expect(r.statusCode, 404); |
| 152 | + expect(r.message, 'Payment code not found'); |
| 153 | + expect(r.value, isNull); |
| 154 | + }); |
| 155 | + |
| 156 | + test('401 with empty body returns typed error', () async { |
| 157 | + stubPost('/follow', '', 401); |
| 158 | + final r = await api.follow('tok', 'sig', 'target'); |
| 159 | + expect(r.statusCode, 401); |
| 160 | + expect(r.message, |
| 161 | + 'Unauthorized token or signature or Unclaimed payment code'); |
| 162 | + expect(r.value, isNull); |
| 163 | + }); |
| 164 | + |
| 165 | + test('400 with empty body returns typed error', () async { |
| 166 | + stubPost('/follow', '', 400); |
| 167 | + final r = await api.follow('tok', 'sig', 'target'); |
| 168 | + expect(r.statusCode, 400); |
| 169 | + expect(r.message, 'Bad request'); |
| 170 | + expect(r.value, isNull); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + group('unfollow', () { |
| 175 | + test('404 with empty body returns typed error', () async { |
| 176 | + stubPost('/unfollow', '', 404); |
| 177 | + final r = await api.unfollow('tok', 'sig', 'target'); |
| 178 | + expect(r.statusCode, 404); |
| 179 | + expect(r.message, 'Payment code not found'); |
| 180 | + expect(r.value, isNull); |
| 181 | + }); |
| 182 | + |
| 183 | + test('401 with empty body returns typed error', () async { |
| 184 | + stubPost('/unfollow', '', 401); |
| 185 | + final r = await api.unfollow('tok', 'sig', 'target'); |
| 186 | + expect(r.statusCode, 401); |
| 187 | + expect(r.message, |
| 188 | + 'Unauthorized token or signature or Unclaimed payment code'); |
| 189 | + expect(r.value, isNull); |
| 190 | + }); |
| 191 | + |
| 192 | + test('400 with empty body returns typed error', () async { |
| 193 | + stubPost('/unfollow', '', 400); |
| 194 | + final r = await api.unfollow('tok', 'sig', 'target'); |
| 195 | + expect(r.statusCode, 400); |
| 196 | + expect(r.message, 'Bad request'); |
| 197 | + expect(r.value, isNull); |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + group('add', () { |
| 202 | + test('400 with empty body returns typed error', () async { |
| 203 | + stubPost('/nym/add', '', 400); |
| 204 | + final r = await api.add('tok', 'sig', 'nym', 'code'); |
| 205 | + expect(r.statusCode, 400); |
| 206 | + expect(r.message, 'Bad request'); |
| 207 | + expect(r.value, false); |
| 208 | + }); |
| 209 | + |
| 210 | + test('401 with empty body returns typed error', () async { |
| 211 | + stubPost('/nym/add', '', 401); |
| 212 | + final r = await api.add('tok', 'sig', 'nym', 'code'); |
| 213 | + expect(r.statusCode, 401); |
| 214 | + expect(r.message, |
| 215 | + 'Unauthorized token or signature or Unclaimed payment code'); |
| 216 | + expect(r.value, false); |
| 217 | + }); |
| 218 | + |
| 219 | + test('404 with empty body returns typed error', () async { |
| 220 | + stubPost('/nym/add', '', 404); |
| 221 | + final r = await api.add('tok', 'sig', 'nym', 'code'); |
| 222 | + expect(r.statusCode, 404); |
| 223 | + expect(r.message, 'Nym not found'); |
| 224 | + expect(r.value, false); |
| 225 | + }); |
| 226 | + }); |
| 227 | +} |
0 commit comments