-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.dart
129 lines (107 loc) · 4.19 KB
/
api_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import 'package:test/test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:todo_as_issue/api/api.dart';
import 'package:todo_as_issue/api/github.dart';
import 'package:todo_as_issue/api/successful_status.dart';
import 'package:todo_as_issue/core/errors/api_exceptions.dart';
import 'package:todo_as_issue/core/http_client/http_client.dart';
import 'package:todo_as_issue/core/http_client/http_client_interface.dart';
import 'mocks/configuration_mock.dart';
import 'mocks/todo_mocks.dart';
class HttpClientMock extends Mock implements HttpClient {}
void main() {
late HttpClientMock httpClientMock;
late GitHub openSourcePlatform;
late String url;
late API api;
setUp(() {
httpClientMock = HttpClientMock();
openSourcePlatform = GitHub(httpClientMock);
url = "/repos/USER/REPOSITORY/issues";
api = API(openSourcePlatform);
});
test("should be a successful message when HTTP status is 201", () async {
when(() => httpClientMock.post(
url,
headers: any(named: "headers"),
body: any(named: "body"),
)).thenAnswer((_) async => HttpResponse(body: {}, statusCode: 201));
final issues =
await api.createIssues(TODOS_MOCK, GITHUB_CONFIGURATION_MOCK);
expect(issues, isA<SuccessfulStatus>());
verify((() => httpClientMock.post(url,
headers: any(named: "headers"),
body: any(named: "body")))).called(TODOS_MOCK.length);
});
test("should throw InvalidCredentials exception when HTTP status is 401",
() async {
when(() => httpClientMock.post(
url,
headers: any(named: "headers"),
body: any(named: "body"),
)).thenAnswer((_) async => HttpResponse(body: {}, statusCode: 401));
expect(
() async => api.createIssues(TODOS_MOCK, GITHUB_CONFIGURATION_MOCK),
throwsA(isA<InvalidCredentials>()),
);
verify((() => httpClientMock.post(url,
headers: any(named: "headers"), body: any(named: "body")))).called(1);
});
test("should throw InvalidCredentials exception when HTTP status is 404",
() async {
when(() => httpClientMock.post(
url,
headers: any(named: "headers"),
body: any(named: "body"),
)).thenAnswer((_) async => HttpResponse(body: {}, statusCode: 404));
expect(
() async => api.createIssues(TODOS_MOCK, GITHUB_CONFIGURATION_MOCK),
throwsA(isA<InvalidCredentials>()),
);
verify((() => httpClientMock.post(url,
headers: any(named: "headers"), body: any(named: "body")))).called(1);
});
test("should throw ServiceUnavaiable exception when HTTP status is 503",
() async {
when(() => httpClientMock.post(
url,
headers: any(named: "headers"),
body: any(named: "body"),
)).thenAnswer((_) async => HttpResponse(body: {}, statusCode: 503));
expect(
() async => api.createIssues(TODOS_MOCK, GITHUB_CONFIGURATION_MOCK),
throwsA(isA<ServiceUnavaiable>()),
);
verify((() => httpClientMock.post(url,
headers: any(named: "headers"), body: any(named: "body")))).called(1);
});
test("should throw SpammedCommand exception when HTTP status is 422",
() async {
when(() => httpClientMock.post(
url,
headers: any(named: "headers"),
body: any(named: "body"),
)).thenAnswer((_) async => HttpResponse(body: {}, statusCode: 422));
expect(
() async => api.createIssues(TODOS_MOCK, GITHUB_CONFIGURATION_MOCK),
throwsA(isA<SpammedCommand>()),
);
verify((() => httpClientMock.post(url,
headers: any(named: "headers"), body: any(named: "body")))).called(1);
});
test(
"should throw UnexpectedError exception when HTTP status is not 401, 404, 503 ou 422.",
() async {
when(() => httpClientMock.post(
url,
headers: any(named: "headers"),
body: any(named: "body"),
)).thenAnswer((_) async => HttpResponse(body: {}, statusCode: 504));
expect(
() async => api.createIssues(TODOS_MOCK, GITHUB_CONFIGURATION_MOCK),
throwsA(isA<UnexpectedError>()),
);
verify((() => httpClientMock.post(url,
headers: any(named: "headers"), body: any(named: "body")))).called(1);
});
}