Skip to content

Commit bde4913

Browse files
dab246hoangdat
authored andcommitted
Add unit-test for get mailbox method
1 parent 7cee708 commit bde4913

File tree

2 files changed

+264
-0
lines changed

2 files changed

+264
-0
lines changed

lib/jmap/mail/mailbox/get/get_mailbox_method.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ class GetMailboxMethod extends GetMethod {
2626
CapabilityIdentifier.jmapMail
2727
};
2828

29+
Set<CapabilityIdentifier> get requiredCapabilitiesSupportTeamMailboxes => {
30+
CapabilityIdentifier.jmapCore,
31+
CapabilityIdentifier.jmapMail,
32+
CapabilityIdentifier.jmapTeamMailboxes
33+
};
34+
2935
@override
3036
List<Object?> get props => [methodName, accountId, ids, properties, requiredCapabilities];
3137

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
import 'package:dio/dio.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:http_mock_adapter/http_mock_adapter.dart';
4+
import 'package:jmap_dart_client/http/http_client.dart';
5+
import 'package:jmap_dart_client/jmap/account_id.dart';
6+
import 'package:jmap_dart_client/jmap/core/id.dart';
7+
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
8+
import 'package:jmap_dart_client/jmap/jmap_request.dart';
9+
import 'package:jmap_dart_client/jmap/mail/mailbox/get/get_mailbox_method.dart';
10+
import 'package:jmap_dart_client/jmap/mail/mailbox/get/get_mailbox_response.dart';
11+
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
12+
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox_rights.dart';
13+
import 'package:jmap_dart_client/jmap/mail/mailbox/namespace.dart';
14+
15+
void main() {
16+
group('test to json get mailbox method', () {
17+
final expectedMailbox1 = Mailbox(
18+
id: MailboxId(Id('f1cef2d0-30a9-11eb-9a8d-254ee97830fe')),
19+
role: Role('inbox'),
20+
name: MailboxName('INBOX'),
21+
sortOrder: SortOrder(sortValue: 10),
22+
totalEmails: TotalEmails(UnsignedInt(1847)),
23+
unreadEmails: UnreadEmails(UnsignedInt(1708)),
24+
totalThreads: TotalThreads(UnsignedInt(1847)),
25+
unreadThreads: UnreadThreads(UnsignedInt(1708)),
26+
myRights: MailboxRights(
27+
true,
28+
true,
29+
true,
30+
true,
31+
true,
32+
true,
33+
true,
34+
true,
35+
true,
36+
),
37+
isSubscribed: IsSubscribed(false),
38+
rights: {
39+
40+
"i",
41+
"l",
42+
"r",
43+
"s",
44+
"t",
45+
"w"
46+
]
47+
},
48+
namespace: Namespace('Personal')
49+
);
50+
51+
final expectedMailbox2 = Mailbox(
52+
id: MailboxId(Id('f1cef2d0-30a9-11eb-9a8d-254ee97830fe')),
53+
role: Role('inbox'),
54+
name: MailboxName('INBOX'),
55+
sortOrder: SortOrder(sortValue: 10),
56+
totalEmails: TotalEmails(UnsignedInt(1847)),
57+
unreadEmails: UnreadEmails(UnsignedInt(1708)),
58+
totalThreads: TotalThreads(UnsignedInt(1847)),
59+
unreadThreads: UnreadThreads(UnsignedInt(1708)),
60+
myRights: MailboxRights(
61+
true,
62+
true,
63+
true,
64+
true,
65+
true,
66+
true,
67+
true,
68+
true,
69+
true,
70+
),
71+
isSubscribed: IsSubscribed(false)
72+
);
73+
74+
test('get mailbox method and response parsing has team mailboxes', () async {
75+
final baseOption = BaseOptions(method: 'POST');
76+
final dio = Dio(baseOption)
77+
..options.baseUrl = 'http://domain.com/jmap';
78+
final dioAdapter = DioAdapter(dio: dio);
79+
dioAdapter.onPost(
80+
'',
81+
(server) => server.reply(200, {
82+
"sessionState": "2c9f1b12-b35a-43e6-9af2-0106fb53a943",
83+
"methodResponses": [
84+
[
85+
"Mailbox/get",
86+
{
87+
"accountId": "0d14dbabe6482aff5cbf922e04cef51a40b4eabccbe12d28fe27c97038752555",
88+
"notFound": [
89+
90+
],
91+
"state": "c7b1bb10-80f5-11ed-b960-5773e4d60b2f",
92+
"list": [
93+
{
94+
"totalThreads": 1847,
95+
"name": "INBOX",
96+
"isSubscribed": false,
97+
"role": "inbox",
98+
"totalEmails": 1847,
99+
"unreadThreads": 1708,
100+
"unreadEmails": 1708,
101+
"sortOrder": 10,
102+
"rights": {
103+
104+
"i",
105+
"l",
106+
"r",
107+
"s",
108+
"t",
109+
"w"
110+
]
111+
},
112+
"namespace": "Personal",
113+
"myRights": {
114+
"mayReadItems": true,
115+
"mayAddItems": true,
116+
"mayRemoveItems": true,
117+
"maySetSeen": true,
118+
"maySetKeywords": true,
119+
"mayCreateChild": true,
120+
"mayRename": true,
121+
"mayDelete": true,
122+
"maySubmit": true
123+
},
124+
"id": "f1cef2d0-30a9-11eb-9a8d-254ee97830fe"
125+
}
126+
]
127+
},
128+
"c0"
129+
]
130+
]
131+
}),
132+
data: {
133+
"using": [
134+
"urn:ietf:params:jmap:core",
135+
"urn:ietf:params:jmap:mail",
136+
"urn:apache:james:params:jmap:mail:shares"
137+
],
138+
"methodCalls": [
139+
[
140+
"Mailbox/get",
141+
{
142+
"accountId": "0d14dbabe6482aff5cbf922e04cef51a40b4eabccbe12d28fe27c97038752555"
143+
},
144+
"c0"
145+
]
146+
]
147+
},
148+
headers: {
149+
"accept": "application/json;jmapVersion=rfc-8621",
150+
"content-type": "application/json; charset=utf-8",
151+
"content-length": 228
152+
}
153+
);
154+
155+
final getMailboxMethod = GetMailboxMethod(AccountId(Id('0d14dbabe6482aff5cbf922e04cef51a40b4eabccbe12d28fe27c97038752555')));
156+
final httpClient = HttpClient(dio);
157+
final requestBuilder = JmapRequestBuilder(httpClient, ProcessingInvocation());
158+
final getMailboxInvocation = requestBuilder.invocation(getMailboxMethod);
159+
final response = await (requestBuilder
160+
..usings(getMailboxMethod.requiredCapabilitiesSupportTeamMailboxes))
161+
.build()
162+
.execute();
163+
164+
final getMailboxResponse = response.parse<GetMailboxResponse>(
165+
getMailboxInvocation.methodCallId,
166+
GetMailboxResponse.deserialize);
167+
168+
expect(getMailboxResponse?.list.length, equals(1));
169+
expect(getMailboxResponse?.list, containsAll([expectedMailbox1]));
170+
});
171+
172+
test('get mailbox method and response parsing not have team mailboxes', () async {
173+
final baseOption = BaseOptions(method: 'POST');
174+
final dio = Dio(baseOption)
175+
..options.baseUrl = 'http://domain.com/jmap';
176+
final dioAdapter = DioAdapter(dio: dio);
177+
dioAdapter.onPost(
178+
'',
179+
(server) => server.reply(200, {
180+
"sessionState": "2c9f1b12-b35a-43e6-9af2-0106fb53a943",
181+
"methodResponses": [
182+
[
183+
"Mailbox/get",
184+
{
185+
"accountId": "0d14dbabe6482aff5cbf922e04cef51a40b4eabccbe12d28fe27c97038752555",
186+
"notFound": [
187+
188+
],
189+
"state": "c7b1bb10-80f5-11ed-b960-5773e4d60b2f",
190+
"list": [
191+
{
192+
"totalThreads": 1847,
193+
"name": "INBOX",
194+
"isSubscribed": false,
195+
"role": "inbox",
196+
"totalEmails": 1847,
197+
"unreadThreads": 1708,
198+
"unreadEmails": 1708,
199+
"sortOrder": 10,
200+
"myRights": {
201+
"mayReadItems": true,
202+
"mayAddItems": true,
203+
"mayRemoveItems": true,
204+
"maySetSeen": true,
205+
"maySetKeywords": true,
206+
"mayCreateChild": true,
207+
"mayRename": true,
208+
"mayDelete": true,
209+
"maySubmit": true
210+
},
211+
"id": "f1cef2d0-30a9-11eb-9a8d-254ee97830fe"
212+
}
213+
]
214+
},
215+
"c0"
216+
]
217+
]
218+
}),
219+
data: {
220+
"using": [
221+
"urn:ietf:params:jmap:core",
222+
"urn:ietf:params:jmap:mail"
223+
],
224+
"methodCalls": [
225+
[
226+
"Mailbox/get",
227+
{
228+
"accountId": "0d14dbabe6482aff5cbf922e04cef51a40b4eabccbe12d28fe27c97038752555"
229+
},
230+
"c0"
231+
]
232+
]
233+
},
234+
headers: {
235+
"accept": "application/json;jmapVersion=rfc-8621",
236+
"content-type": "application/json; charset=utf-8",
237+
"content-length": 185
238+
}
239+
);
240+
241+
final getMailboxMethod = GetMailboxMethod(AccountId(Id('0d14dbabe6482aff5cbf922e04cef51a40b4eabccbe12d28fe27c97038752555')));
242+
final httpClient = HttpClient(dio);
243+
final requestBuilder = JmapRequestBuilder(httpClient, ProcessingInvocation());
244+
final getMailboxInvocation = requestBuilder.invocation(getMailboxMethod);
245+
final response = await (requestBuilder
246+
..usings(getMailboxMethod.requiredCapabilities))
247+
.build()
248+
.execute();
249+
250+
final getMailboxResponse = response.parse<GetMailboxResponse>(
251+
getMailboxInvocation.methodCallId,
252+
GetMailboxResponse.deserialize);
253+
254+
expect(getMailboxResponse?.list.length, equals(1));
255+
expect(getMailboxResponse?.list, containsAll([expectedMailbox2]));
256+
});
257+
});
258+
}

0 commit comments

Comments
 (0)