Skip to content

Commit c00d581

Browse files
committed
Added end-to-end test for googleapis_auth
1 parent 33db43a commit c00d581

File tree

10 files changed

+619
-4
lines changed

10 files changed

+619
-4
lines changed

.github/workflows/dart.yml

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ jobs:
149149
uses: actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306
150150
with:
151151
path: "~/.pub-cache/hosted"
152-
key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:_test-_test_package-discoveryapis_commons-discoveryapis_generator-generated/googleapis-generated/googleapis_beta-generator-googleapis_auth-test_integration;commands:format-analyze_0"
152+
key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:_test-_test_package-discoveryapis_commons-discoveryapis_generator-generated/googleapis-generated/googleapis_beta-generator-googleapis_auth-test_integration;commands:format-analyze_2"
153153
restore-keys: |
154154
os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:_test-_test_package-discoveryapis_commons-discoveryapis_generator-generated/googleapis-generated/googleapis_beta-generator-googleapis_auth-test_integration
155155
os:ubuntu-latest;pub-cache-hosted;sdk:dev
@@ -755,3 +755,103 @@ jobs:
755755
- job_001
756756
- job_002
757757
- job_003
758+
job_018:
759+
name: "analyze_format; Dart 3.9.0; PKG: _googleapis_auth_e2e; `dart analyze`"
760+
runs-on: ubuntu-latest
761+
steps:
762+
- name: Cache Pub hosted dependencies
763+
uses: actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306
764+
with:
765+
path: "~/.pub-cache/hosted"
766+
key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.9.0;packages:_googleapis_auth_e2e;commands:analyze_1"
767+
restore-keys: |
768+
os:ubuntu-latest;pub-cache-hosted;sdk:3.9.0;packages:_googleapis_auth_e2e
769+
os:ubuntu-latest;pub-cache-hosted;sdk:3.9.0
770+
os:ubuntu-latest;pub-cache-hosted
771+
os:ubuntu-latest
772+
- name: Setup Dart SDK
773+
uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c
774+
with:
775+
sdk: "3.9.0"
776+
- id: checkout
777+
name: Checkout repository
778+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
779+
- id: _googleapis_auth_e2e_pub_upgrade
780+
name: _googleapis_auth_e2e; dart pub upgrade
781+
run: dart pub upgrade
782+
if: "always() && steps.checkout.conclusion == 'success'"
783+
working-directory: _googleapis_auth_e2e
784+
- name: _googleapis_auth_e2e; dart analyze
785+
run: dart analyze
786+
if: "always() && steps._googleapis_auth_e2e_pub_upgrade.conclusion == 'success'"
787+
working-directory: _googleapis_auth_e2e
788+
needs:
789+
- job_001
790+
- job_002
791+
- job_003
792+
- job_004
793+
- job_005
794+
- job_006
795+
- job_007
796+
- job_008
797+
- job_009
798+
- job_010
799+
- job_011
800+
- job_012
801+
- job_013
802+
- job_014
803+
- job_015
804+
- job_016
805+
- job_017
806+
job_019:
807+
name: "analyze_format; Dart dev; PKG: _googleapis_auth_e2e; `dart format --output=none --set-exit-if-changed .`, `dart analyze --fatal-infos`"
808+
runs-on: ubuntu-latest
809+
steps:
810+
- name: Cache Pub hosted dependencies
811+
uses: actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306
812+
with:
813+
path: "~/.pub-cache/hosted"
814+
key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:_googleapis_auth_e2e;commands:format-analyze_0"
815+
restore-keys: |
816+
os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:_googleapis_auth_e2e
817+
os:ubuntu-latest;pub-cache-hosted;sdk:dev
818+
os:ubuntu-latest;pub-cache-hosted
819+
os:ubuntu-latest
820+
- name: Setup Dart SDK
821+
uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c
822+
with:
823+
sdk: dev
824+
- id: checkout
825+
name: Checkout repository
826+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
827+
- id: _googleapis_auth_e2e_pub_upgrade
828+
name: _googleapis_auth_e2e; dart pub upgrade
829+
run: dart pub upgrade
830+
if: "always() && steps.checkout.conclusion == 'success'"
831+
working-directory: _googleapis_auth_e2e
832+
- name: "_googleapis_auth_e2e; dart format --output=none --set-exit-if-changed ."
833+
run: "dart format --output=none --set-exit-if-changed ."
834+
if: "always() && steps._googleapis_auth_e2e_pub_upgrade.conclusion == 'success'"
835+
working-directory: _googleapis_auth_e2e
836+
- name: "_googleapis_auth_e2e; dart analyze --fatal-infos"
837+
run: dart analyze --fatal-infos
838+
if: "always() && steps._googleapis_auth_e2e_pub_upgrade.conclusion == 'success'"
839+
working-directory: _googleapis_auth_e2e
840+
needs:
841+
- job_001
842+
- job_002
843+
- job_003
844+
- job_004
845+
- job_005
846+
- job_006
847+
- job_007
848+
- job_008
849+
- job_009
850+
- job_010
851+
- job_011
852+
- job_012
853+
- job_013
854+
- job_014
855+
- job_015
856+
- job_016
857+
- job_017

_googleapis_auth_e2e/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
4+
import 'package:google_cloud/google_cloud.dart';
5+
import 'package:googleapis/oauth2/v2.dart';
6+
import 'package:googleapis_auth/auth_io.dart';
7+
import 'package:shelf/shelf.dart';
8+
import 'package:shelf_router/shelf_router.dart';
9+
10+
Future<void> main() async {
11+
final router = Router();
12+
13+
router.get('/user_info', (Request request) async {
14+
try {
15+
final client = await clientViaApplicationDefaultCredentials(
16+
scopes: [Oauth2Api.userinfoEmailScope],
17+
);
18+
try {
19+
final api = Oauth2Api(client);
20+
final userInfo = await api.userinfo.get();
21+
return Response.ok(
22+
'User: ${userInfo.email}',
23+
headers: {'content-type': 'text/plain'},
24+
);
25+
} finally {
26+
client.close();
27+
}
28+
} catch (e, stack) {
29+
print('Error: $e\n$stack');
30+
return Response.internalServerError(body: 'Auth failed: $e');
31+
}
32+
});
33+
34+
router.get('/sign_blob', (Request request) async {
35+
try {
36+
// Need a purely authenticated client (no specific scopes needed for IAM
37+
// signing itself usually, but 'cloud-platform' is mentioned in docs).
38+
// However, clientViaApplicationDefaultCredentials requires scopes.
39+
final client = await clientViaApplicationDefaultCredentials(
40+
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
41+
);
42+
43+
String? email;
44+
final envCreds = Platform.environment['GOOGLE_APPLICATION_CREDENTIALS'];
45+
if (envCreds != null && File(envCreds).existsSync()) {
46+
try {
47+
final json = jsonDecode(File(envCreds).readAsStringSync());
48+
if (json is Map && json['type'] == 'service_account') {
49+
email = json['client_email'] as String?;
50+
}
51+
} catch (e) {
52+
print('Failed to parse creds file: $e');
53+
}
54+
}
55+
56+
try {
57+
final result = await signBlob(
58+
client,
59+
utf8.encode('Hello World'),
60+
serviceAccountEmail: email,
61+
);
62+
return Response.ok(
63+
jsonEncode({'signedBlob': result.signedBlob, 'keyId': result.keyId}),
64+
headers: {'content-type': 'application/json'},
65+
);
66+
} finally {
67+
client.close();
68+
}
69+
} catch (e, stack) {
70+
if (e.toString().contains('metadata.google.internal')) {
71+
print('Skipping signBlob: Metadata server unavailable');
72+
return Response(412, body: 'Metadata server unavailable');
73+
}
74+
print('Error signing blob: $e\n$stack');
75+
return Response.internalServerError(body: 'Sign blob failed: $e');
76+
}
77+
});
78+
79+
final handler = const Pipeline()
80+
.addMiddleware(logRequests())
81+
.addHandler(router.call);
82+
83+
await serveHandler(handler);
84+
}

_googleapis_auth_e2e/mono_pkg.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# https://github.com/google/mono_repo.dart
2+
3+
stages:
4+
- analyze_format:
5+
- group:
6+
- format
7+
- analyze: --fatal-infos
8+
sdk: dev
9+
- analyze:
10+
sdk: pubspec

_googleapis_auth_e2e/pubspec.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: _googleapis_auth_e2e
2+
description: End-to-end validation for the googleapis_auth package.
3+
publish_to: none
4+
resolution: workspace
5+
6+
environment:
7+
sdk: ^3.9.0
8+
9+
dependencies:
10+
google_cloud: any
11+
googleapis: any
12+
googleapis_auth: any
13+
http: ^1.2.0
14+
shelf: ^1.4.0
15+
shelf_router: ^1.1.0
16+
17+
dev_dependencies:
18+
args: ^2.7.0
19+
path: ^1.9.1
20+
test: ^1.25.0
21+
yaml: ^3.1.3
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
4+
import 'package:http/http.dart' as http;
5+
import 'package:test/test.dart';
6+
7+
void main() {
8+
final urlEnv = Platform.environment['E2E_URL'];
9+
10+
group('E2E Validation', () {
11+
final url =
12+
urlEnv ?? 'http://localhost:${Platform.environment['PORT'] ?? '8080'}';
13+
final rootUri = Uri.parse(url);
14+
15+
test('get user_info', () async {
16+
final response = await http.get(rootUri.replace(path: '/user_info'));
17+
expect(response.statusCode, 200);
18+
print('User Info: ${response.body}');
19+
});
20+
21+
test('sign_blob', () async {
22+
final response = await http.get(rootUri.replace(path: '/sign_blob'));
23+
if (response.statusCode == 412) {
24+
print(
25+
'Skipping sign_blob test: Metadata server unavailable (local env)',
26+
);
27+
return;
28+
}
29+
expect(response.statusCode, 200, reason: response.body);
30+
final json = jsonDecode(response.body) as Map<String, dynamic>;
31+
expect(json, contains('signedBlob'));
32+
expect(json, contains('keyId'));
33+
print('Signed Blob JSON: $json');
34+
});
35+
});
36+
}

0 commit comments

Comments
 (0)