You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
samples/experimental/desktop_photo_search/test/unsplash_test.dart

348 lines
12 KiB

// Copyright 2019 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:desktop_photo_search/src/unsplash/photo.dart';
import 'package:desktop_photo_search/src/unsplash/search_photos_response.dart';
import 'package:desktop_photo_search/src/unsplash/unsplash.dart';
import 'package:desktop_photo_search/src/unsplash/user.dart';
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:test/test.dart';
void main() {
test('Photo.fromJson', () {
const input = '''
{
"id": "Dwu85P9SOIk",
"created_at": "2016-05-03T11:00:28-04:00",
"updated_at": "2016-07-10T11:00:01-05:00",
"width": 2448,
"height": 3264,
"color": "#6E633A",
"downloads": 1345,
"likes": 24,
"liked_by_user": false,
"description": "A man drinking a coffee.",
"exif": {
"make": "Canon",
"model": "Canon EOS 40D",
"exposure_time": "0.011111111111111112",
"aperture": "4.970854",
"focal_length": "37",
"iso": 100
},
"location": {
"city": "Montreal",
"country": "Canada",
"position": {
"latitude": 45.4732984,
"longitude": -73.6384879
}
},
"tags": [
{ "title": "man" },
{ "title": "drinking" },
{ "title": "coffee" }
],
"current_user_collections": [
{
"id": 206,
"title": "Makers: Cat and Ben",
"published_at": "2016-01-12T18:16:09-05:00",
"updated_at": "2016-07-10T11:00:01-05:00",
"cover_photo": null,
"user": null
}
],
"urls": {
"raw": "https://images.unsplash.com/photo-1417325384643-aac51acc9e5d",
"full": "https://images.unsplash.com/photo-1417325384643-aac51acc9e5d?q=75&fm=jpg",
"regular": "https://images.unsplash.com/photo-1417325384643-aac51acc9e5d?q=75&fm=jpg&w=1080&fit=max",
"small": "https://images.unsplash.com/photo-1417325384643-aac51acc9e5d?q=75&fm=jpg&w=400&fit=max",
"thumb": "https://images.unsplash.com/photo-1417325384643-aac51acc9e5d?q=75&fm=jpg&w=200&fit=max"
},
"links": {
"self": "https://api.unsplash.com/photos/Dwu85P9SOIk",
"html": "https://unsplash.com/photos/Dwu85P9SOIk",
"download": "https://unsplash.com/photos/Dwu85P9SOIk/download",
"download_location": "https://api.unsplash.com/photos/Dwu85P9SOIk/download"
},
"user": {
"id": "QPxL2MGqfrw",
"updated_at": "2016-07-10T11:00:01-05:00",
"username": "exampleuser",
"name": "Joe Example",
"portfolio_url": "https://example.com/",
"bio": "Just an everyday Joe",
"location": "Montreal",
"total_likes": 5,
"total_photos": 10,
"total_collections": 13,
"links": {
"self": "https://api.unsplash.com/users/exampleuser",
"html": "https://unsplash.com/exampleuser",
"photos": "https://api.unsplash.com/users/exampleuser/photos",
"likes": "https://api.unsplash.com/users/exampleuser/likes",
"portfolio": "https://api.unsplash.com/users/exampleuser/portfolio"
}
}
}
''';
final photo = Photo.fromJson(input);
expect(photo.id, 'Dwu85P9SOIk');
expect(photo.createdAt, '2016-05-03T11:00:28-04:00');
expect(photo.updatedAt, '2016-07-10T11:00:01-05:00');
expect(photo.width, 2448);
expect(photo.height, 3264);
expect(photo.color, '#6E633A');
expect(photo.downloads, 1345);
expect(photo.likedByUser, false);
expect(photo.exif.make, 'Canon');
expect(photo.exif.iso, 100);
expect(photo.location.city, 'Montreal');
expect(photo.location.country, 'Canada');
expect(photo.location.position.latitude, 45.4732984);
expect(photo.location.position.longitude, -73.6384879);
});
test('User.fromJson', () {
const input = '''
{
"id": "pXhwzz1JtQU",
"updated_at": "2016-07-10T11:00:01-05:00",
"username": "jimmyexample",
"name": "James Example",
"first_name": "James",
"last_name": "Example",
"instagram_username": "instantgrammer",
"twitter_username": "jimmy",
"portfolio_url": null,
"bio": "The user's bio",
"location": "Montreal, Qc",
"total_likes": 20,
"total_photos": 10,
"total_collections": 5,
"followed_by_user": false,
"followers_count": 300,
"following_count": 25,
"downloads": 225974,
"profile_image": {
"small": "https://images.unsplash.com/face-springmorning.jpg?q=80&fm=jpg&crop=faces&fit=crop&h=32&w=32",
"medium": "https://images.unsplash.com/face-springmorning.jpg?q=80&fm=jpg&crop=faces&fit=crop&h=64&w=64",
"large": "https://images.unsplash.com/face-springmorning.jpg?q=80&fm=jpg&crop=faces&fit=crop&h=128&w=128"
},
"badge": {
"title": "Book contributor",
"primary": true,
"slug": "book-contributor",
"link": "https://book.unsplash.com"
},
"links": {
"self": "https://api.unsplash.com/users/jimmyexample",
"html": "https://unsplash.com/jimmyexample",
"photos": "https://api.unsplash.com/users/jimmyexample/photos",
"likes": "https://api.unsplash.com/users/jimmyexample/likes",
"portfolio": "https://api.unsplash.com/users/jimmyexample/portfolio"
}
}
''';
final user = User.fromJson(input);
expect(user.id, 'pXhwzz1JtQU');
});
test('SearchPhotosResponse.fromJson', () {
const input = '''
{
"total": 133,
"total_pages": 7,
"results": [
{
"id": "eOLpJytrbsQ",
"created_at": "2014-11-18T14:35:36-05:00",
"width": 4000,
"height": 3000,
"color": "#A7A2A1",
"likes": 286,
"liked_by_user": false,
"description": "A man drinking a coffee.",
"user": {
"id": "Ul0QVz12Goo",
"username": "ugmonk",
"name": "Jeff Sheldon",
"first_name": "Jeff",
"last_name": "Sheldon",
"instagram_username": "instantgrammer",
"twitter_username": "ugmonk",
"portfolio_url": "http://ugmonk.com/",
"profile_image": {
"small": "https://images.unsplash.com/profile-1441298803695-accd94000cac?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=32&w=32&s=7cfe3b93750cb0c93e2f7caec08b5a41",
"medium": "https://images.unsplash.com/profile-1441298803695-accd94000cac?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=64&w=64&s=5a9dc749c43ce5bd60870b129a40902f",
"large": "https://images.unsplash.com/profile-1441298803695-accd94000cac?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=128&w=128&s=32085a077889586df88bfbe406692202"
},
"links": {
"self": "https://api.unsplash.com/users/ugmonk",
"html": "http://unsplash.com/@ugmonk",
"photos": "https://api.unsplash.com/users/ugmonk/photos",
"likes": "https://api.unsplash.com/users/ugmonk/likes"
}
},
"current_user_collections": [],
"urls": {
"raw": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f",
"full": "https://hd.unsplash.com/photo-1416339306562-f3d12fefd36f",
"regular": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&s=92f3e02f63678acc8416d044e189f515",
"small": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=263af33585f9d32af39d165b000845eb",
"thumb": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max&s=8aae34cf35df31a592f0bef16e6342ef"
},
"links": {
"self": "https://api.unsplash.com/photos/eOLpJytrbsQ",
"html": "http://unsplash.com/photos/eOLpJytrbsQ",
"download": "http://unsplash.com/photos/eOLpJytrbsQ/download"
}
}
]
}
''';
final response = SearchPhotosResponse.fromJson(input);
expect(response.total, 133);
expect(response.totalPages, 7);
expect(response.results[0].id, 'eOLpJytrbsQ');
expect(response.results[0].user.id, 'Ul0QVz12Goo');
});
group('Unsplash API client', () {
test('handles success', () async {
const searchPhotosResponseBody = '''
{
"total": 133,
"total_pages": 7,
"results": [
{
"id": "eOLpJytrbsQ",
"created_at": "2014-11-18T14:35:36-05:00",
"width": 4000,
"height": 3000,
"color": "#A7A2A1",
"likes": 286,
"liked_by_user": false,
"description": "A man drinking a coffee.",
"user": {
"id": "Ul0QVz12Goo",
"username": "ugmonk",
"name": "Jeff Sheldon",
"first_name": "Jeff",
"last_name": "Sheldon",
"instagram_username": "instantgrammer",
"twitter_username": "ugmonk",
"portfolio_url": "http://ugmonk.com/",
"profile_image": {
"small": "https://images.unsplash.com/profile-1441298803695-accd94000cac?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=32&w=32&s=7cfe3b93750cb0c93e2f7caec08b5a41",
"medium": "https://images.unsplash.com/profile-1441298803695-accd94000cac?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=64&w=64&s=5a9dc749c43ce5bd60870b129a40902f",
"large": "https://images.unsplash.com/profile-1441298803695-accd94000cac?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=128&w=128&s=32085a077889586df88bfbe406692202"
},
"links": {
"self": "https://api.unsplash.com/users/ugmonk",
"html": "http://unsplash.com/@ugmonk",
"photos": "https://api.unsplash.com/users/ugmonk/photos",
"likes": "https://api.unsplash.com/users/ugmonk/likes"
}
},
"current_user_collections": [],
"urls": {
"raw": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f",
"full": "https://hd.unsplash.com/photo-1416339306562-f3d12fefd36f",
"regular": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&s=92f3e02f63678acc8416d044e189f515",
"small": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=263af33585f9d32af39d165b000845eb",
"thumb": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max&s=8aae34cf35df31a592f0bef16e6342ef"
},
"links": {
"self": "https://api.unsplash.com/photos/eOLpJytrbsQ",
"html": "http://unsplash.com/photos/eOLpJytrbsQ",
"download": "http://unsplash.com/photos/eOLpJytrbsQ/download"
}
}
]
}
''';
final httpClient = MockClient((req) async {
return Response(
searchPhotosResponseBody,
200,
request: req,
headers: {'content-type': 'application/json'},
);
});
final unsplashClient = Unsplash(
accessKey: 'not-an-access-key',
httpClient: httpClient,
);
final response = await unsplashClient.searchPhotos(query: 'red');
expect(response.total, 133);
expect(response.totalPages, 7);
expect(response.results[0].id, 'eOLpJytrbsQ');
expect(response.results[0].user.id, 'Ul0QVz12Goo');
});
test('handles failure', () async {
const apiError =
'{"errors":["OAuth error: The access token is invalid"]}';
final httpClient = MockClient((req) async {
return Response(
apiError,
401,
request: req,
headers: {'content-type': 'application/json'},
);
});
final unsplashClient = Unsplash(
accessKey: 'not-an-access-key',
httpClient: httpClient,
);
try {
await unsplashClient.searchPhotos(query: 'red');
fail('UnsplashException should have been thrown');
} on UnsplashException catch (e) {
expect(e.toString(),
'UnsplashException: OAuth error: The access token is invalid');
}
});
test('handles broken JSON', () async {
const apiError = '{"tot'; // truncated JSON.
final httpClient = MockClient((req) async {
return Response(
apiError,
401,
request: req,
headers: {'content-type': 'application/json'},
);
});
final unsplashClient = Unsplash(
accessKey: 'not-an-access-key',
httpClient: httpClient,
);
try {
await unsplashClient.searchPhotos(query: 'red');
fail('UnsplashException should have been thrown');
} on UnsplashException catch (e) {
expect(e.toString(),
'UnsplashException: Invalid JSON received');
}
});
});
}