| 1 | #!/bin/bash |
| 2 | # Copyright 2026 Google LLC |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | # Integration Test: Fallback OAuth Configuration Verification |
| 17 | # |
| 18 | # This test verifies that the CLI correctly falls back to using the bundled |
| 19 | # oauth_config.json containing the default client ID when no local config is present. |
| 20 | # It simulates a fresh user flow by temporarily hiding existing tokens/configs, |
| 21 | # starting the OAuth flow, and asserting that the printed authorization URL |
| 22 | # contains the correct Client ID. |
| 23 | # |
| 24 | |
| 25 | set -e |
| 26 | |
| 27 | TOKEN_PATH="$HOME/.config/colab-cli/token.json" |
| 28 | CONFIG_PATH="$HOME/.colab-cli-oauth-config.json" |
| 29 | BACKUP_SUFFIX=".backup.$(date +%s)" |
| 30 | |
| 31 | TOKEN_BACKUP="" |
| 32 | CONFIG_BACKUP="" |
| 33 | |
| 34 | cleanup() { |
| 35 | echo "[*] Cleaning up backups..." |
| 36 | if [ -n "$TOKEN_BACKUP" ] && [ -f "$TOKEN_BACKUP" ]; then |
| 37 | mv "$TOKEN_BACKUP" "$TOKEN_PATH" |
| 38 | echo "[*] Restored token.json" |
| 39 | fi |
| 40 | if [ -n "$CONFIG_BACKUP" ] && [ -f "$CONFIG_BACKUP" ]; then |
| 41 | mv "$CONFIG_BACKUP" "$CONFIG_PATH" |
| 42 | echo "[*] Restored .colab-cli-oauth-config.json" |
| 43 | fi |
| 44 | } |
| 45 | trap cleanup EXIT |
| 46 | |
| 47 | # 1. Back up existing configuration files if they exist |
| 48 | if [ -f "$TOKEN_PATH" ]; then |
| 49 | TOKEN_BACKUP="${TOKEN_PATH}${BACKUP_SUFFIX}" |
| 50 | mv "$TOKEN_PATH" "$TOKEN_BACKUP" |
| 51 | echo "[*] Temporarily backed up token.json to $TOKEN_BACKUP" |
| 52 | fi |
| 53 | |
| 54 | if [ -f "$CONFIG_PATH" ]; then |
| 55 | CONFIG_BACKUP="${CONFIG_PATH}${BACKUP_SUFFIX}" |
| 56 | mv "$CONFIG_PATH" "$CONFIG_BACKUP" |
| 57 | echo "[*] Temporarily backed up .colab-cli-oauth-config.json to $CONFIG_BACKUP" |
| 58 | fi |
| 59 | |
| 60 | # 2. Run colab sessions command to trigger the OAuth flow |
| 61 | echo "[*] Running 'colab --auth=oauth2 sessions' (expecting to trigger browser flow)..." |
| 62 | # We expect the command to block waiting for authorization, so we run it with a timeout. |
| 63 | # We redirect output to a file so we can inspect it. |
| 64 | OUTPUT_LOG=$(mktemp) |
| 65 | set +e |
| 66 | PYTHONUNBUFFERED=1 timeout 5 uv run colab --auth=oauth2 sessions > "$OUTPUT_LOG" 2>&1 |
| 67 | EXIT_CODE=$? |
| 68 | set -e |
| 69 | |
| 70 | echo "[*] Command exited with code: $EXIT_CODE" |
| 71 | echo "----------------- CLI Output -----------------" |
| 72 | cat "$OUTPUT_LOG" |
| 73 | echo "----------------------------------------------" |
| 74 | |
| 75 | # 3. Assertions |
| 76 | EXPECTED_CLIENT_ID="764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com" |
| 77 | |
| 78 | # The command should have printed the authorization URL |
| 79 | if ! grep -q "Please visit this URL to authorize this application" "$OUTPUT_LOG"; then |
| 80 | echo "[FAILURE] OAuth prompt message not found in output." |
| 81 | rm -f "$OUTPUT_LOG" |
| 82 | exit 1 |
| 83 | fi |
| 84 | |
| 85 | # The URL should contain the correct client ID |
| 86 | if ! grep -q "client_id=$EXPECTED_CLIENT_ID" "$OUTPUT_LOG"; then |
| 87 | echo "[FAILURE] Authorization URL does not contain the expected client ID: $EXPECTED_CLIENT_ID" |
| 88 | rm -f "$OUTPUT_LOG" |
| 89 | exit 1 |
| 90 | fi |
| 91 | |
| 92 | echo "[SUCCESS] Verified that CLI correctly initiated OAuth flow with the fallback Client ID: $EXPECTED_CLIENT_ID" |
| 93 | rm -f "$OUTPUT_LOG" |