Skip to content

Commit 2c21beb

Browse files
author
Sergey Khomushin
committed
add private key
1 parent aea5aaa commit 2c21beb

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

lib/src/api/send_json.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import 'package:http/http.dart' as http;
22
import '../models/emailjs_response_status.dart';
33

4-
Future<EmailJSResponseStatus> sendJSON(http.Client client, Uri uri, String data) async {
4+
/// sends JSON object via HTTP POST
5+
Future<EmailJSResponseStatus> sendJSON(
6+
http.Client client, Uri uri, String data) async {
57
try {
68
var response = await client.post(
79
uri,

lib/src/models/emailjs_response_status.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// Response status from EmailJS API
12
class EmailJSResponseStatus {
23
int status = 0;
34
String text = 'Network Error';

lib/src/types/options.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
abstract class Options {
2-
String publicKey = '';
3-
String? privateKey;
1+
class Options {
2+
/// The public key is passed unmodified to API call.
3+
final String publicKey;
4+
5+
/// The private key is passed unmodified to API call.
6+
final String? privateKey;
7+
8+
const Options({
9+
this.publicKey = '',
10+
this.privateKey,
11+
});
412
}

lib/src/utils/validate_params.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// Validates required params
12
bool validateParams(
23
String publicKey,
34
String serviceID,

test/emailjs_test.dart

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import 'package:flutter_test/flutter_test.dart';
22
import 'package:mocktail/mocktail.dart';
33
import 'package:http/http.dart' as http;
44

5+
import 'package:emailjs/src/models/emailjs_response_status.dart';
6+
import 'package:emailjs/src/types/options.dart';
57
import 'package:emailjs/emailjs.dart';
68

79
class MockClient extends Mock implements http.Client {}
@@ -22,7 +24,10 @@ void main() {
2224
});
2325

2426
test('should send method and fail on the service ID', () {
25-
EmailJS.init('LC2JWGTestKeySomething');
27+
EmailJS.init(const Options(
28+
publicKey: 'LC2JWGTestKeySomething',
29+
privateKey: 'PrKeyTestKeySomething',
30+
));
2631

2732
expect(
2833
() => EmailJS.send('', 'my_test_template'),
@@ -31,7 +36,10 @@ void main() {
3136
});
3237

3338
test('should send method and fail on the template ID', () {
34-
EmailJS.init('LC2JWGTestKeySomething');
39+
EmailJS.init(const Options(
40+
publicKey: 'LC2JWGTestKeySomething',
41+
privateKey: 'PrKeyTestKeySomething',
42+
));
3543

3644
expect(
3745
() => EmailJS.send('default_service', ''),
@@ -50,14 +58,22 @@ void main() {
5058
body: any(named: 'body'),
5159
)).thenAnswer((_) async => http.Response('OK', 200));
5260

53-
EmailJS.init('LC2JWGTestKeySomething', null, mockHttpClient);
61+
EmailJS.init(
62+
const Options(
63+
publicKey: 'LC2JWGTestKeySomething',
64+
privateKey: 'PrKeyTestKeySomething',
65+
),
66+
null,
67+
mockHttpClient,
68+
);
5469

5570
try {
5671
final result = await EmailJS.send(
5772
'default_service',
5873
'my_test_template',
5974
);
60-
expect(result, equals('OK'));
75+
expect(result.status, 200);
76+
expect(result.text, 'OK');
6177
} catch (error) {
6278
expect(error, isNull);
6379
}
@@ -73,16 +89,25 @@ void main() {
7389
)).thenAnswer((_) async => http.Response('OK', 200));
7490

7591
// pass the mock http client
76-
EmailJS.init('', null, mockHttpClient);
92+
EmailJS.init(
93+
const Options(
94+
publicKey: '',
95+
),
96+
null,
97+
mockHttpClient,
98+
);
7799

78100
try {
79101
final result = await EmailJS.send(
80102
'default_service',
81103
'my_test_template',
82104
null,
83-
'LC2JWGTestKeySomething',
84-
);
85-
expect(result, equals('OK'));
105+
const Options(
106+
publicKey: 'LC2JWGTestKeySomething',
107+
privateKey: 'PrKeyTestKeySomething',
108+
));
109+
expect(result.status, 200);
110+
expect(result.text, 'OK');
86111
} catch (error) {
87112
expect(error, isNull);
88113
}
@@ -98,7 +123,14 @@ void main() {
98123
)).thenAnswer((_) async => http.Response('The Public Key is required', 403));
99124

100125
// pass the mock http client
101-
EmailJS.init('LC2JWGTestKeySomething', null, mockHttpClient);
126+
EmailJS.init(
127+
const Options(
128+
publicKey: 'LC2JWGTestKeySomething',
129+
privateKey: 'PrKeyTestKeySomething',
130+
),
131+
null,
132+
mockHttpClient,
133+
);
102134

103135
try {
104136
final result = await EmailJS.send(
@@ -107,7 +139,10 @@ void main() {
107139
);
108140
expect(result, isNull);
109141
} catch (error) {
110-
expect(error, equals('The Public Key is required'));
142+
if (error is EmailJSResponseStatus) {
143+
expect(error.status, 403);
144+
expect(error.text, 'The Public Key is required');
145+
}
111146
}
112147
});
113148
});

0 commit comments

Comments
 (0)