feat: Add tests, update notes implementation

This commit is contained in:
Anton Stubenbord
2023-12-31 15:26:20 +01:00
parent d7f297a4df
commit 55aa42e4ab
29 changed files with 273 additions and 115 deletions

View File

@@ -0,0 +1,90 @@
import 'package:dio/dio.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
import 'package:mockito/mockito.dart';
import 'package:paperless_api/paperless_api.dart';
void main() {
group('AuthenticationApi with DioHttpErrorIncerceptor', () {
late PaperlessAuthenticationApi authenticationApi;
late DioAdapter mockAdapter;
const token = "abcde";
const invalidCredentialsServerMessage =
"Unable to log in with provided credentials.";
setUp(() {
final dio = Dio()..interceptors.add(DioHttpErrorInterceptor());
authenticationApi = PaperlessAuthenticationApiImpl(dio);
mockAdapter = DioAdapter(dio: dio);
// Valid credentials
mockAdapter.onPost(
"/api/token/",
data: {
"username": "username",
"password": "password",
},
(server) => server.reply(200, {"token": token}),
);
// Invalid credentials
mockAdapter.onPost(
"/api/token/",
data: {
"username": "wrongUsername",
"password": "wrongPassword",
},
(server) => server.reply(400, {
"non_field_errors": [invalidCredentialsServerMessage]
}),
);
});
// tearDown(() {});
test(
'should return a valid token when logging in with valid credentials',
() {
expect(
authenticationApi.login(
username: "username",
password: "password",
),
completion(token),
);
},
);
test(
'should throw a PaperlessFormValidationException containing a reason '
'when logging in with invalid credentials',
() {
expect(
authenticationApi.login(
username: "wrongUsername",
password: "wrongPassword",
),
throwsA(isA<PaperlessFormValidationException>().having(
(e) => e.unspecificErrorMessage(),
"non-field specific error message",
equals(invalidCredentialsServerMessage),
)),
);
},
);
test(
'should return an error when logging in with invalid credentials',
() {
expect(
authenticationApi.login(
username: "wrongUsername",
password: "wrongPassword",
),
throwsA(isA<PaperlessFormValidationException>().having(
(e) => e.unspecificErrorMessage(),
"non-field specific error message",
equals(invalidCredentialsServerMessage),
)),
);
},
);
});
}

View File

@@ -2,7 +2,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:paperless_api/paperless_api.dart';
void main() {
group('Validate parsing logic from [SavedView] to [DocumentFilter]:', () {
group('Parsing [SavedView] to [DocumentFilter]:', () {
test('Values are correctly parsed if set.', () {
expect(
SavedView.fromJson({
@@ -64,7 +64,7 @@ void main() {
]
}).toDocumentFilter(),
equals(
DocumentFilter.initial.copyWith(
DocumentFilter(
correspondent: const SetIdQueryParameter(id: 42),
documentType: const SetIdQueryParameter(id: 69),
storagePath: const SetIdQueryParameter(id: 14),
@@ -83,6 +83,7 @@ void main() {
sortField: SortField.created,
sortOrder: SortOrder.descending,
query: const TextQuery.extended("Never gonna give you up"),
selectedView: 1,
),
),
);
@@ -99,7 +100,11 @@ void main() {
"sort_reverse": true,
"filter_rules": [],
}).toDocumentFilter(),
equals(DocumentFilter.initial),
equals(
const DocumentFilter(
selectedView: 1,
),
),
);
});
@@ -130,11 +135,12 @@ void main() {
},
],
}).toDocumentFilter();
final expected = DocumentFilter.initial.copyWith(
correspondent: const NotAssignedIdQueryParameter(),
documentType: const NotAssignedIdQueryParameter(),
storagePath: const NotAssignedIdQueryParameter(),
tags: const NotAssignedTagsQuery(),
const expected = DocumentFilter(
correspondent: NotAssignedIdQueryParameter(),
documentType: NotAssignedIdQueryParameter(),
storagePath: NotAssignedIdQueryParameter(),
tags: NotAssignedTagsQuery(),
selectedView: 1,
);
expect(
actual,
@@ -148,6 +154,7 @@ void main() {
expect(
SavedView.fromDocumentFilter(
DocumentFilter(
selectedView: 1,
correspondent: const SetIdQueryParameter(id: 1),
documentType: const SetIdQueryParameter(id: 2),
storagePath: const SetIdQueryParameter(id: 3),
@@ -173,6 +180,7 @@ void main() {
),
equals(
SavedView(
id: 1,
name: "test_name",
showOnDashboard: false,
showInSidebar: false,