Browse Source

nextcloud: Clean responses in tests only if necessary

pull/23/head
jld3103 2 years ago
parent
commit
e93f1b6839
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 19
      packages/nextcloud/test/helper.dart
  2. 23
      packages/nextcloud/test/news_test.dart
  3. 2
      packages/nextcloud/test/provisioning_api_test.dart
  4. 13
      packages/nextcloud/test/user_status_test.dart

19
packages/nextcloud/test/helper.dart

@ -286,14 +286,24 @@ class TestDockerHelper {
'RUN ./occ app:install $appName'; 'RUN ./occ app:install $appName';
} }
Future<T?> validateResponse<T, U>(final ApiInstance api, final Future<Response> input) async { /// Validates that the response matches the schema
///
/// [cleanResponse] can be used for compatibility reasons, because some APIs are very inconsistent in their responses and don't populate all fields
Future<T?> validateResponse<T, U>(
final ApiInstance api,
final Future<Response> input, {
final bool cleanResponse = false,
}) async {
final response = await input; final response = await input;
if (response.statusCode >= HttpStatus.badRequest) { if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await decodeBodyBytes(response)); throw ApiException(response.statusCode, await decodeBodyBytes(response));
} }
if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) { if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
final body = removeNulls(json.decode(await decodeBodyBytes(response))); var body = json.decode(await decodeBodyBytes(response));
if (cleanResponse) {
body = removeNulls(body);
}
var output = await api.apiClient.deserializeAsync( var output = await api.apiClient.deserializeAsync(
json.encode(body), json.encode(body),
T.toString(), T.toString(),
@ -303,7 +313,10 @@ Future<T?> validateResponse<T, U>(final ApiInstance api, final Future<Response>
} }
output = output as T; output = output as T;
final parsedBody = removeNulls(json.decode(json.encode(output))); var parsedBody = json.decode(json.encode(output));
if (cleanResponse) {
parsedBody = removeNulls(parsedBody);
}
expect(parsedBody, body); expect(parsedBody, body);
return output; return output;

23
packages/nextcloud/test/news_test.dart

@ -24,6 +24,7 @@ Future main() async {
folderId: folderID, folderId: folderID,
), ),
), ),
cleanResponse: true,
))!; ))!;
Future<NewsListFeeds> addNasaFeed() async => (await validateResponse<NewsListFeeds, void>( Future<NewsListFeeds> addNasaFeed() async => (await validateResponse<NewsListFeeds, void>(
@ -31,6 +32,7 @@ Future main() async {
client.news.addFeedWithHttpInfo( client.news.addFeedWithHttpInfo(
NewsAddFeed(url: nasaFeedURL), NewsAddFeed(url: nasaFeedURL),
), ),
cleanResponse: true,
))!; ))!;
test('Add feed', () async { test('Add feed', () async {
@ -51,6 +53,7 @@ Future main() async {
response = (await validateResponse<NewsListFeeds, void>( response = (await validateResponse<NewsListFeeds, void>(
client.news, client.news,
client.news.listFeedsWithHttpInfo(), client.news.listFeedsWithHttpInfo(),
cleanResponse: true,
))!; ))!;
expect(response.starredCount, 0); expect(response.starredCount, 0);
expect(response.newestItemId, isNotNull); expect(response.newestItemId, isNotNull);
@ -64,6 +67,7 @@ Future main() async {
var articlesResponse = (await validateResponse<NewsListArticles, void>( var articlesResponse = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 6), client.news.listArticlesWithHttpInfo(type: 6),
cleanResponse: true,
))!; ))!;
expect(articlesResponse.items.length, greaterThan(0)); expect(articlesResponse.items.length, greaterThan(0));
@ -77,6 +81,7 @@ Future main() async {
articlesResponse = (await validateResponse<NewsListArticles, void>( articlesResponse = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 6), client.news.listArticlesWithHttpInfo(type: 6),
cleanResponse: true,
))!; ))!;
expect(articlesResponse.items, hasLength(0)); expect(articlesResponse.items, hasLength(0));
}); });
@ -85,6 +90,7 @@ Future main() async {
var response = (await validateResponse<NewsListArticles, void>( var response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(), client.news.listArticlesWithHttpInfo(),
cleanResponse: true,
))!; ))!;
expect(response.items, hasLength(0)); expect(response.items, hasLength(0));
@ -93,6 +99,7 @@ Future main() async {
response = (await validateResponse<NewsListArticles, void>( response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(), client.news.listArticlesWithHttpInfo(),
cleanResponse: true,
))!; ))!;
expect(response.items.length, greaterThan(0)); expect(response.items.length, greaterThan(0));
expect(response.items[0].body, isNotNull); expect(response.items[0].body, isNotNull);
@ -112,6 +119,7 @@ Future main() async {
var response = (await validateResponse<NewsListArticles, void>( var response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(), client.news.listArticlesWithHttpInfo(),
cleanResponse: true,
))!; ))!;
final wikipediaArticles = response.items.length; final wikipediaArticles = response.items.length;
expect(wikipediaArticles, greaterThan(0)); expect(wikipediaArticles, greaterThan(0));
@ -121,6 +129,7 @@ Future main() async {
response = (await validateResponse<NewsListArticles, void>( response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(), client.news.listArticlesWithHttpInfo(),
cleanResponse: true,
))!; ))!;
final nasaArticles = response.items.length - wikipediaArticles; final nasaArticles = response.items.length - wikipediaArticles;
expect(nasaArticles, greaterThan(0)); expect(nasaArticles, greaterThan(0));
@ -130,6 +139,7 @@ Future main() async {
client.news.listUpdatedArticlesWithHttpInfo( client.news.listUpdatedArticlesWithHttpInfo(
lastModified: response.items[response.items.length - 1 - nasaArticles].lastModified, lastModified: response.items[response.items.length - 1 - nasaArticles].lastModified,
), ),
cleanResponse: true,
))!; ))!;
expect(response.items, hasLength(nasaArticles)); expect(response.items, hasLength(nasaArticles));
}); });
@ -140,6 +150,7 @@ Future main() async {
var response = (await validateResponse<NewsListArticles, void>( var response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 6), client.news.listArticlesWithHttpInfo(type: 6),
cleanResponse: true,
))!; ))!;
final unreadArticles = response.items.length; final unreadArticles = response.items.length;
expect(unreadArticles, greaterThan(0)); expect(unreadArticles, greaterThan(0));
@ -148,6 +159,7 @@ Future main() async {
response = (await validateResponse<NewsListArticles, void>( response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 6), client.news.listArticlesWithHttpInfo(type: 6),
cleanResponse: true,
))!; ))!;
expect(response.items, hasLength(unreadArticles - 1)); expect(response.items, hasLength(unreadArticles - 1));
}); });
@ -158,12 +170,14 @@ Future main() async {
var response = (await validateResponse<NewsListArticles, void>( var response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 6), client.news.listArticlesWithHttpInfo(type: 6),
cleanResponse: true,
))!; ))!;
final readArticle = response.items[0]; final readArticle = response.items[0];
await client.news.markArticleAsRead(readArticle.id!); await client.news.markArticleAsRead(readArticle.id!);
response = (await validateResponse<NewsListArticles, void>( response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 6), client.news.listArticlesWithHttpInfo(type: 6),
cleanResponse: true,
))!; ))!;
final unreadArticles = response.items.length; final unreadArticles = response.items.length;
expect(unreadArticles, greaterThan(0)); expect(unreadArticles, greaterThan(0));
@ -172,6 +186,7 @@ Future main() async {
response = (await validateResponse<NewsListArticles, void>( response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 6), client.news.listArticlesWithHttpInfo(type: 6),
cleanResponse: true,
))!; ))!;
expect(response.items, hasLength(unreadArticles + 1)); expect(response.items, hasLength(unreadArticles + 1));
}); });
@ -182,6 +197,7 @@ Future main() async {
var response = (await validateResponse<NewsListArticles, void>( var response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 2), client.news.listArticlesWithHttpInfo(type: 2),
cleanResponse: true,
))!; ))!;
final starredArticles = response.items.length; final starredArticles = response.items.length;
expect(starredArticles, 0); expect(starredArticles, 0);
@ -189,11 +205,13 @@ Future main() async {
response = (await validateResponse<NewsListArticles, void>( response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(), client.news.listArticlesWithHttpInfo(),
cleanResponse: true,
))!; ))!;
await client.news.starArticle(response.items[0].feedId!, response.items[0].guidHash!); await client.news.starArticle(response.items[0].feedId!, response.items[0].guidHash!);
response = (await validateResponse<NewsListArticles, void>( response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 2), client.news.listArticlesWithHttpInfo(type: 2),
cleanResponse: true,
))!; ))!;
expect(response.items, hasLength(1)); expect(response.items, hasLength(1));
}); });
@ -204,6 +222,7 @@ Future main() async {
var response = (await validateResponse<NewsListArticles, void>( var response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(), client.news.listArticlesWithHttpInfo(),
cleanResponse: true,
))!; ))!;
final item = response.items[0]; final item = response.items[0];
@ -211,6 +230,7 @@ Future main() async {
response = (await validateResponse<NewsListArticles, void>( response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 2), client.news.listArticlesWithHttpInfo(type: 2),
cleanResponse: true,
))!; ))!;
expect(response.items, hasLength(1)); expect(response.items, hasLength(1));
@ -218,6 +238,7 @@ Future main() async {
response = (await validateResponse<NewsListArticles, void>( response = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 2), client.news.listArticlesWithHttpInfo(type: 2),
cleanResponse: true,
))!; ))!;
expect(response.items, hasLength(0)); expect(response.items, hasLength(0));
}); });
@ -295,6 +316,7 @@ Future main() async {
var articlesResponse = (await validateResponse<NewsListArticles, void>( var articlesResponse = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 6), client.news.listArticlesWithHttpInfo(type: 6),
cleanResponse: true,
))!; ))!;
expect(articlesResponse.items.length, greaterThan(0)); expect(articlesResponse.items.length, greaterThan(0));
@ -308,6 +330,7 @@ Future main() async {
articlesResponse = (await validateResponse<NewsListArticles, void>( articlesResponse = (await validateResponse<NewsListArticles, void>(
client.news, client.news,
client.news.listArticlesWithHttpInfo(type: 6), client.news.listArticlesWithHttpInfo(type: 6),
cleanResponse: true,
))!; ))!;
expect(articlesResponse.items, hasLength(0)); expect(articlesResponse.items, hasLength(0));
}); });

2
packages/nextcloud/test/provisioning_api_test.dart

@ -17,6 +17,7 @@ Future main() async {
final user = (await validateResponse<ProvisioningApiUser, void>( final user = (await validateResponse<ProvisioningApiUser, void>(
client.provisioningApi, client.provisioningApi,
client.provisioningApi.getCurrentUserWithHttpInfo(), client.provisioningApi.getCurrentUserWithHttpInfo(),
cleanResponse: true,
))!; ))!;
expect(user.ocs!.data!.id, 'test'); expect(user.ocs!.data!.id, 'test');
expect(user.ocs!.data!.getDisplayName(), 'Test'); expect(user.ocs!.data!.getDisplayName(), 'Test');
@ -28,6 +29,7 @@ Future main() async {
final user = (await validateResponse<ProvisioningApiUser, void>( final user = (await validateResponse<ProvisioningApiUser, void>(
client.provisioningApi, client.provisioningApi,
client.provisioningApi.getUserWithHttpInfo('test'), client.provisioningApi.getUserWithHttpInfo('test'),
cleanResponse: true,
))!; ))!;
expect(user.ocs!.data!.id, 'test'); expect(user.ocs!.data!.id, 'test');
expect(user.ocs!.data!.getDisplayName(), 'Test'); expect(user.ocs!.data!.getDisplayName(), 'Test');

13
packages/nextcloud/test/user_status_test.dart

@ -18,6 +18,7 @@ Future main() async {
final response = (await validateResponse<UserStatusPredefinedStatuses, void>( final response = (await validateResponse<UserStatusPredefinedStatuses, void>(
client.userStatus, client.userStatus,
client.userStatus.findAllPredefinedStatusesWithHttpInfo(), client.userStatus.findAllPredefinedStatusesWithHttpInfo(),
cleanResponse: true,
))!; ))!;
expect(response.ocs!.data, hasLength(5)); expect(response.ocs!.data, hasLength(5));
@ -55,6 +56,7 @@ Future main() async {
final response = (await validateResponse<UserStatusGetUserStatus, void>( final response = (await validateResponse<UserStatusGetUserStatus, void>(
client.userStatus, client.userStatus,
client.userStatus.setStatusWithHttpInfo(UserStatusSetUserStatus(statusType: UserStatusTypeEnum.online)), client.userStatus.setStatusWithHttpInfo(UserStatusSetUserStatus(statusType: UserStatusTypeEnum.online)),
cleanResponse: true,
))!; ))!;
expect(response.ocs!.data!.userId, 'test'); expect(response.ocs!.data!.userId, 'test');
@ -74,10 +76,12 @@ Future main() async {
await validateResponse<UserStatusGetUserStatus, void>( await validateResponse<UserStatusGetUserStatus, void>(
client.userStatus, client.userStatus,
client.userStatus.setStatusWithHttpInfo(UserStatusSetUserStatus(statusType: UserStatusTypeEnum.online)), client.userStatus.setStatusWithHttpInfo(UserStatusSetUserStatus(statusType: UserStatusTypeEnum.online)),
cleanResponse: true,
); );
final response = (await validateResponse<UserStatusGetUserStatus, void>( final response = (await validateResponse<UserStatusGetUserStatus, void>(
client.userStatus, client.userStatus,
client.userStatus.getStatusWithHttpInfo(), client.userStatus.getStatusWithHttpInfo(),
cleanResponse: true,
))!; ))!;
expect(response.ocs!.data!.userId, 'test'); expect(response.ocs!.data!.userId, 'test');
@ -100,11 +104,13 @@ Future main() async {
await validateResponse<UserStatusGetUserStatus, void>( await validateResponse<UserStatusGetUserStatus, void>(
client.userStatus, client.userStatus,
client.userStatus.setStatusWithHttpInfo(UserStatusSetUserStatus(statusType: UserStatusTypeEnum.online)), client.userStatus.setStatusWithHttpInfo(UserStatusSetUserStatus(statusType: UserStatusTypeEnum.online)),
cleanResponse: true,
); );
response = (await validateResponse<UserStatusFindAllStatuses, void>( response = (await validateResponse<UserStatusFindAllStatuses, void>(
client.userStatus, client.userStatus,
client.userStatus.findAllStatusesWithHttpInfo(), client.userStatus.findAllStatusesWithHttpInfo(),
cleanResponse: true,
))!; ))!;
expect(response.ocs!.data, hasLength(1)); expect(response.ocs!.data, hasLength(1));
expect(response.ocs!.data[0].userId, 'test'); expect(response.ocs!.data[0].userId, 'test');
@ -119,11 +125,13 @@ Future main() async {
await validateResponse<UserStatusGetUserStatus, void>( await validateResponse<UserStatusGetUserStatus, void>(
client.userStatus, client.userStatus,
client.userStatus.setStatusWithHttpInfo(UserStatusSetUserStatus(statusType: UserStatusTypeEnum.online)), client.userStatus.setStatusWithHttpInfo(UserStatusSetUserStatus(statusType: UserStatusTypeEnum.online)),
cleanResponse: true,
); );
final response = (await validateResponse<UserStatusFindStatus, void>( final response = (await validateResponse<UserStatusFindStatus, void>(
client.userStatus, client.userStatus,
client.userStatus.findStatusWithHttpInfo('test'), client.userStatus.findStatusWithHttpInfo('test'),
cleanResponse: true,
))!; ))!;
expect(response.ocs!.data!.userId, 'test'); expect(response.ocs!.data!.userId, 'test');
expect(response.ocs!.data!.message, null); expect(response.ocs!.data!.message, null);
@ -142,6 +150,7 @@ Future main() async {
clearAt: clearAt, clearAt: clearAt,
), ),
), ),
cleanResponse: true,
))!; ))!;
expect(response.ocs!.data!.userId, 'test'); expect(response.ocs!.data!.userId, 'test');
@ -165,6 +174,7 @@ Future main() async {
clearAt: clearAt, clearAt: clearAt,
), ),
), ),
cleanResponse: true,
))!; ))!;
expect(response.ocs!.data!.userId, 'test'); expect(response.ocs!.data!.userId, 'test');
@ -188,12 +198,14 @@ Future main() async {
clearAt: clearAt, clearAt: clearAt,
), ),
), ),
cleanResponse: true,
); );
await client.userStatus.clearMessage(); await client.userStatus.clearMessage();
final response = (await validateResponse<UserStatusGetUserStatus, void>( final response = (await validateResponse<UserStatusGetUserStatus, void>(
client.userStatus, client.userStatus,
client.userStatus.getStatusWithHttpInfo(), client.userStatus.getStatusWithHttpInfo(),
cleanResponse: true,
))!; ))!;
expect(response.ocs!.data!.userId, 'test'); expect(response.ocs!.data!.userId, 'test');
expect(response.ocs!.data!.message, null); expect(response.ocs!.data!.message, null);
@ -210,6 +222,7 @@ Future main() async {
final response = (await validateResponse<UserStatusGetUserStatus, void>( final response = (await validateResponse<UserStatusGetUserStatus, void>(
client.userStatus, client.userStatus,
client.userStatus.getStatusWithHttpInfo(), client.userStatus.getStatusWithHttpInfo(),
cleanResponse: true,
))!; ))!;
expect(response.ocs!.data!.userId, 'test'); expect(response.ocs!.data!.userId, 'test');

Loading…
Cancel
Save