|
| 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/filter/filter.dart'; |
| 7 | +import 'package:jmap_dart_client/jmap/core/filter/filter_operator.dart'; |
| 8 | +import 'package:jmap_dart_client/jmap/core/filter/operator/query_filter_operator.dart'; |
| 9 | +import 'package:jmap_dart_client/jmap/core/id.dart'; |
| 10 | +import 'package:jmap_dart_client/jmap/core/properties/properties.dart'; |
| 11 | +import 'package:jmap_dart_client/jmap/core/request/reference_path.dart'; |
| 12 | +import 'package:jmap_dart_client/jmap/core/request/request_invocation.dart'; |
| 13 | +import 'package:jmap_dart_client/jmap/core/unsigned_int.dart'; |
| 14 | +import 'package:jmap_dart_client/jmap/core/utc_date.dart'; |
| 15 | +import 'package:jmap_dart_client/jmap/jmap_request.dart'; |
| 16 | +import 'package:jmap_dart_client/jmap/mail/email/email.dart'; |
| 17 | +import 'package:jmap_dart_client/jmap/mail/email/email_address.dart'; |
| 18 | +import 'package:jmap_dart_client/jmap/mail/email/email_comparator.dart'; |
| 19 | +import 'package:jmap_dart_client/jmap/mail/email/email_comparator_property.dart'; |
| 20 | +import 'package:jmap_dart_client/jmap/mail/email/email_filter_condition.dart'; |
| 21 | +import 'package:jmap_dart_client/jmap/mail/email/get/get_email_method.dart'; |
| 22 | +import 'package:jmap_dart_client/jmap/mail/email/get/get_email_response.dart'; |
| 23 | +import 'package:jmap_dart_client/jmap/mail/email/query/query_email_method.dart'; |
| 24 | + |
| 25 | +void main() { |
| 26 | + group('get list email test', () { |
| 27 | + final expectMail1 = Email( |
| 28 | + EmailId(Id("382312d0-fa5c-11eb-b647-2fef1ee78d9e")), |
| 29 | + preview: "Dear QA,I attached image here", |
| 30 | + hasAttachment: false, |
| 31 | + subject: "test inline image", |
| 32 | + from : { EmailAddress( "DatPH", "[email protected]")}, |
| 33 | + sentAt: UTCDate(DateTime.parse("2021-08-11T04:25:34Z")), |
| 34 | + receivedAt: UTCDate(DateTime.parse("2021-08-11T04:25:55Z"))); |
| 35 | + |
| 36 | + final expectMail2 = Email( |
| 37 | + EmailId(Id("bc8a5320-fa58-11eb-b647-2fef1ee78d9e")), |
| 38 | + preview: |
| 39 | + "This event is about to begin Noti check TimeFriday 23 October 2020 12:00 - 12:30 Europe/Paris (See in Calendar)Location1 thai ha (See in Map)Attendees - User A <[email protected]> (Organizer) - Lê Nguyễn <[email protected]> - User C <[email protected]", |
| 40 | + hasAttachment: false, |
| 41 | + subject: "Notification: Noti check", |
| 42 | + from : { EmailAddress( null, "[email protected]")}, |
| 43 | + sentAt: UTCDate(DateTime.parse("2021-08-10T09:45:01Z")), |
| 44 | + receivedAt: UTCDate(DateTime.parse("2021-08-11T04:00:59Z"))); |
| 45 | + |
| 46 | + final expectMail3 = Email( |
| 47 | + EmailId(Id("ba7e0860-fa58-11eb-b647-2fef1ee78d9e")), |
| 48 | + preview: |
| 49 | + "This event is about to begin Recurrencr TimeWednesday 26 August 2020 05:30 - 06:30 Europe/Paris (See in Calendar)Location1 thai ha (See in Map)Attendees - [email protected] <[email protected]> (Organizer) - User A <[email protected]> Resourc", |
| 50 | + hasAttachment: false, |
| 51 | + subject: "Notification: Recurrencr", |
| 52 | + from : { EmailAddress( null, "[email protected]")}, |
| 53 | + sentAt: UTCDate(DateTime.parse("2021-08-11T03:00:00Z")), |
| 54 | + receivedAt: UTCDate(DateTime.parse("2021-08-11T04:00:55Z"))); |
| 55 | + |
| 56 | + final expectMail4 = Email( |
| 57 | + EmailId(Id("d9b3b880-fa6f-11eb-b647-2fef1ee78d9e")), |
| 58 | + preview: "alo -- desktop signature", |
| 59 | + hasAttachment: true, |
| 60 | + subject: "test attachment", |
| 61 | + from : { EmailAddress( "Haaheoo", "[email protected]")}, |
| 62 | + sentAt: UTCDate(DateTime.parse("2021-08-11T06:46:25Z")), |
| 63 | + receivedAt: UTCDate(DateTime.parse("2021-08-11T06:46:26Z"))); |
| 64 | + |
| 65 | + final expectMail5 = Email( |
| 66 | + EmailId(Id("637f1ef0-fa5d-11eb-b647-2fef1ee78d9e")), |
| 67 | + preview: "Dear, test inline Thanks and BRs-- desktop signature", |
| 68 | + hasAttachment: false, |
| 69 | + subject: "test inline image", |
| 70 | + from : { EmailAddress( "Haaheoo", "[email protected]")}, |
| 71 | + sentAt: UTCDate(DateTime.parse("2021-08-11T04:34:13Z")), |
| 72 | + receivedAt: UTCDate(DateTime.parse("2021-08-11T04:34:17Z"))); |
| 73 | + |
| 74 | + test('get email in a mailbox correctly', () async { |
| 75 | + final baseOption = BaseOptions(method: 'POST'); |
| 76 | + final dio = Dio(baseOption)..options.baseUrl = 'http://domain.com'; |
| 77 | + final dioAdapter = DioAdapter(dio: dio); |
| 78 | + dioAdapter.onPost( |
| 79 | + '/jmap', |
| 80 | + (server) => server.reply(200, { |
| 81 | + "sessionState": "2c9f1b12-b35a-43e6-9af2-0106fb53a943", |
| 82 | + "methodResponses": [ |
| 83 | + [ |
| 84 | + "Email/query", |
| 85 | + { |
| 86 | + "accountId": |
| 87 | + "3ce33c876a726662c627746eb9537a1d13c2338193ef27bd051a3ce5c0fe5b12", |
| 88 | + "queryState": "248a9026", |
| 89 | + "canCalculateChanges": false, |
| 90 | + "ids": [ |
| 91 | + "d9b3b880-fa6f-11eb-b647-2fef1ee78d9e", |
| 92 | + "637f1ef0-fa5d-11eb-b647-2fef1ee78d9e", |
| 93 | + "382312d0-fa5c-11eb-b647-2fef1ee78d9e", |
| 94 | + "ba7e0860-fa58-11eb-b647-2fef1ee78d9e", |
| 95 | + "bc8a5320-fa58-11eb-b647-2fef1ee78d9e" |
| 96 | + ], |
| 97 | + "position": 0 |
| 98 | + }, |
| 99 | + "c2" |
| 100 | + ], |
| 101 | + [ |
| 102 | + "Email/get", |
| 103 | + { |
| 104 | + "accountId": |
| 105 | + "3ce33c876a726662c627746eb9537a1d13c2338193ef27bd051a3ce5c0fe5b12", |
| 106 | + "notFound": [], |
| 107 | + "state": "05114a40-fa7c-11eb-a9c2-2fef1ee78d9e", |
| 108 | + "list": [ |
| 109 | + { |
| 110 | + "preview": "Dear QA,I attached image here", |
| 111 | + "hasAttachment": false, |
| 112 | + "subject": "test inline image", |
| 113 | + "from": [ |
| 114 | + { |
| 115 | + "name": "DatPH", |
| 116 | + |
| 117 | + } |
| 118 | + ], |
| 119 | + "sentAt": "2021-08-11T04:25:34Z", |
| 120 | + "id": "382312d0-fa5c-11eb-b647-2fef1ee78d9e", |
| 121 | + "receivedAt": "2021-08-11T04:25:55Z" |
| 122 | + }, |
| 123 | + { |
| 124 | + "preview": |
| 125 | + "This event is about to begin Noti check TimeFriday 23 October 2020 12:00 - 12:30 Europe/Paris (See in Calendar)Location1 thai ha (See in Map)Attendees - User A <[email protected]> (Organizer) - Lê Nguyễn <[email protected]> - User C <[email protected]", |
| 126 | + "hasAttachment": false, |
| 127 | + "subject": "Notification: Noti check", |
| 128 | + "from": [ |
| 129 | + |
| 130 | + ], |
| 131 | + "sentAt": "2021-08-10T09:45:01Z", |
| 132 | + "id": "bc8a5320-fa58-11eb-b647-2fef1ee78d9e", |
| 133 | + "receivedAt": "2021-08-11T04:00:59Z" |
| 134 | + }, |
| 135 | + { |
| 136 | + "preview": |
| 137 | + "This event is about to begin Recurrencr TimeWednesday 26 August 2020 05:30 - 06:30 Europe/Paris (See in Calendar)Location1 thai ha (See in Map)Attendees - [email protected] <[email protected]> (Organizer) - User A <[email protected]> Resourc", |
| 138 | + "hasAttachment": false, |
| 139 | + "subject": "Notification: Recurrencr", |
| 140 | + "from": [ |
| 141 | + |
| 142 | + ], |
| 143 | + "sentAt": "2021-08-11T03:00:00Z", |
| 144 | + "id": "ba7e0860-fa58-11eb-b647-2fef1ee78d9e", |
| 145 | + "receivedAt": "2021-08-11T04:00:55Z" |
| 146 | + }, |
| 147 | + { |
| 148 | + "preview": "alo -- desktop signature", |
| 149 | + "hasAttachment": true, |
| 150 | + "subject": "test attachment", |
| 151 | + "from": [ |
| 152 | + { |
| 153 | + "name": "Haaheoo", |
| 154 | + |
| 155 | + } |
| 156 | + ], |
| 157 | + "sentAt": "2021-08-11T06:46:25Z", |
| 158 | + "id": "d9b3b880-fa6f-11eb-b647-2fef1ee78d9e", |
| 159 | + "receivedAt": "2021-08-11T06:46:26Z" |
| 160 | + }, |
| 161 | + { |
| 162 | + "preview": |
| 163 | + "Dear, test inline Thanks and BRs-- desktop signature", |
| 164 | + "hasAttachment": false, |
| 165 | + "subject": "test inline image", |
| 166 | + "from": [ |
| 167 | + { |
| 168 | + "name": "Haaheoo", |
| 169 | + |
| 170 | + } |
| 171 | + ], |
| 172 | + "sentAt": "2021-08-11T04:34:13Z", |
| 173 | + "id": "637f1ef0-fa5d-11eb-b647-2fef1ee78d9e", |
| 174 | + "receivedAt": "2021-08-11T04:34:17Z" |
| 175 | + } |
| 176 | + ] |
| 177 | + }, |
| 178 | + "c3" |
| 179 | + ] |
| 180 | + ] |
| 181 | + }), |
| 182 | + data: { |
| 183 | + "using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"], |
| 184 | + "methodCalls": [ |
| 185 | + [ |
| 186 | + "Email/query", |
| 187 | + { |
| 188 | + "accountId": |
| 189 | + "93c56f4408cff66f0a929aea8e3940e753c3275e5622582ae3010e7277b7696c", |
| 190 | + "filter": { |
| 191 | + "operator": "OR", |
| 192 | + "conditions": [ |
| 193 | + {"hasKeyword": "music"}, |
| 194 | + {"hasKeyword": "video"} |
| 195 | + ] |
| 196 | + }, |
| 197 | + "sort": [ |
| 198 | + {"isAscending": false, "property": "sentAt"} |
| 199 | + ], |
| 200 | + "limit": 20, |
| 201 | + }, |
| 202 | + "c2" |
| 203 | + ], |
| 204 | + [ |
| 205 | + "Email/get", |
| 206 | + { |
| 207 | + "accountId": |
| 208 | + "93c56f4408cff66f0a929aea8e3940e753c3275e5622582ae3010e7277b7696c", |
| 209 | + "#ids": { |
| 210 | + "resultOf": "c2", |
| 211 | + "name": "Email/query", |
| 212 | + "path": "ids/*" |
| 213 | + }, |
| 214 | + "properties": [ |
| 215 | + "id", |
| 216 | + "subject", |
| 217 | + "from", |
| 218 | + "receivedAt", |
| 219 | + "sentAt", |
| 220 | + "preview", |
| 221 | + "hasAttachment" |
| 222 | + ] |
| 223 | + }, |
| 224 | + "c3" |
| 225 | + ] |
| 226 | + ] |
| 227 | + }, |
| 228 | + headers: { |
| 229 | + "accept": "application/json;jmapVersion=rfc-8621", |
| 230 | + "content-type": "application/json; charset=utf-8", |
| 231 | + "content-length": 581 |
| 232 | + }); |
| 233 | + |
| 234 | + final httpClient = HttpClient(dio); |
| 235 | + final processingInvocation = ProcessingInvocation(); |
| 236 | + final jmapRequestBuilder = |
| 237 | + JmapRequestBuilder(httpClient, processingInvocation); |
| 238 | + final accountId = AccountId(Id( |
| 239 | + '93c56f4408cff66f0a929aea8e3940e753c3275e5622582ae3010e7277b7696c')); |
| 240 | + |
| 241 | + final queryEmailMethod = QueryEmailMethod(accountId) |
| 242 | + ..addLimit(UnsignedInt(20)) |
| 243 | + ..addSorts({ |
| 244 | + EmailComparator(EmailComparatorProperty.sentAt)..setIsAscending(false) |
| 245 | + }) |
| 246 | + ..addFilters(QueryFilterOperator( |
| 247 | + Operator.OR.name, |
| 248 | + Set<Filter>.from([ |
| 249 | + EmailFilterCondition(hasKeyword: "music"), |
| 250 | + EmailFilterCondition(hasKeyword: "video"), |
| 251 | + ]))); |
| 252 | + final queryEmailInvocation = jmapRequestBuilder |
| 253 | + .invocation(queryEmailMethod, methodCallId: MethodCallId('c2')); |
| 254 | + |
| 255 | + final getEmailMethod = GetEmailMethod(accountId) |
| 256 | + ..addProperties(Properties({ |
| 257 | + "id", |
| 258 | + "subject", |
| 259 | + "from", |
| 260 | + "receivedAt", |
| 261 | + "sentAt", |
| 262 | + "preview", |
| 263 | + "hasAttachment" |
| 264 | + })) |
| 265 | + ..addReferenceIds(processingInvocation.createResultReference( |
| 266 | + queryEmailInvocation.methodCallId, ReferencePath.idsPath)); |
| 267 | + final getEmailInvocation = jmapRequestBuilder.invocation(getEmailMethod, |
| 268 | + methodCallId: MethodCallId('c3')); |
| 269 | + |
| 270 | + final result = await (jmapRequestBuilder |
| 271 | + ..usings(getEmailMethod.requiredCapabilities)) |
| 272 | + .build() |
| 273 | + .execute(); |
| 274 | + |
| 275 | + final resultList = result.parse<GetEmailResponse>( |
| 276 | + getEmailInvocation.methodCallId, GetEmailResponse.deserialize); |
| 277 | + |
| 278 | + expect(resultList?.list.length, equals(5)); |
| 279 | + expect( |
| 280 | + resultList?.list, |
| 281 | + containsAll({ |
| 282 | + expectMail1, |
| 283 | + expectMail2, |
| 284 | + expectMail3, |
| 285 | + expectMail4, |
| 286 | + expectMail5 |
| 287 | + })); |
| 288 | + }); |
| 289 | + }); |
| 290 | +} |
0 commit comments