Skip to content

Commit 528e9dd

Browse files
committed
test(paynym): add PayNym.rs POST tests for endpoints
TODO: refactor `paynym_is`->`paynym_rs`
1 parent 0471b6d commit 528e9dd

2 files changed

Lines changed: 326 additions & 0 deletions

File tree

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
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+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Mocks generated by Mockito 5.4.6 from annotations
2+
// in stackwallet/test/services/paynym/paynym_is_api_test.dart.
3+
// Do not manually edit this file.
4+
5+
// ignore_for_file: no_leading_underscores_for_library_prefixes
6+
import 'dart:async' as _i3;
7+
import 'dart:convert' as _i5;
8+
import 'dart:io' as _i4;
9+
10+
import 'package:mockito/mockito.dart' as _i1;
11+
import 'package:stackwallet/networking/http.dart' as _i2;
12+
13+
// ignore_for_file: type=lint
14+
// ignore_for_file: avoid_redundant_argument_values
15+
// ignore_for_file: avoid_setters_without_getters
16+
// ignore_for_file: comment_references
17+
// ignore_for_file: deprecated_member_use
18+
// ignore_for_file: deprecated_member_use_from_same_package
19+
// ignore_for_file: implementation_imports
20+
// ignore_for_file: invalid_use_of_visible_for_testing_member
21+
// ignore_for_file: must_be_immutable
22+
// ignore_for_file: prefer_const_constructors
23+
// ignore_for_file: unnecessary_parenthesis
24+
// ignore_for_file: camel_case_types
25+
// ignore_for_file: subtype_of_sealed_class
26+
// ignore_for_file: invalid_use_of_internal_member
27+
28+
class _FakeResponse_0 extends _i1.SmartFake implements _i2.Response {
29+
_FakeResponse_0(Object parent, Invocation parentInvocation)
30+
: super(parent, parentInvocation);
31+
}
32+
33+
/// A class which mocks [HTTP].
34+
///
35+
/// See the documentation for Mockito's code generation for more information.
36+
class MockHTTP extends _i1.Mock implements _i2.HTTP {
37+
MockHTTP() {
38+
_i1.throwOnMissingStub(this);
39+
}
40+
41+
@override
42+
_i3.Future<_i2.Response> get({
43+
required Uri? url,
44+
Map<String, String>? headers,
45+
required ({_i4.InternetAddress host, int port})? proxyInfo,
46+
Duration? connectionTimeout,
47+
}) =>
48+
(super.noSuchMethod(
49+
Invocation.method(#get, [], {
50+
#url: url,
51+
#headers: headers,
52+
#proxyInfo: proxyInfo,
53+
#connectionTimeout: connectionTimeout,
54+
}),
55+
returnValue: _i3.Future<_i2.Response>.value(
56+
_FakeResponse_0(
57+
this,
58+
Invocation.method(#get, [], {
59+
#url: url,
60+
#headers: headers,
61+
#proxyInfo: proxyInfo,
62+
#connectionTimeout: connectionTimeout,
63+
}),
64+
),
65+
),
66+
)
67+
as _i3.Future<_i2.Response>);
68+
69+
@override
70+
_i3.Future<_i2.Response> post({
71+
required Uri? url,
72+
Map<String, String>? headers,
73+
Object? body,
74+
_i5.Encoding? encoding,
75+
required ({_i4.InternetAddress host, int port})? proxyInfo,
76+
}) =>
77+
(super.noSuchMethod(
78+
Invocation.method(#post, [], {
79+
#url: url,
80+
#headers: headers,
81+
#body: body,
82+
#encoding: encoding,
83+
#proxyInfo: proxyInfo,
84+
}),
85+
returnValue: _i3.Future<_i2.Response>.value(
86+
_FakeResponse_0(
87+
this,
88+
Invocation.method(#post, [], {
89+
#url: url,
90+
#headers: headers,
91+
#body: body,
92+
#encoding: encoding,
93+
#proxyInfo: proxyInfo,
94+
}),
95+
),
96+
),
97+
)
98+
as _i3.Future<_i2.Response>);
99+
}

0 commit comments

Comments
 (0)