diff --git a/.gitignore b/.gitignore index 693d4769dec..525ddb9b7b6 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,7 @@ yarn.lock .DS_Store .idea *.iml +*.tgz +scripts/*.json lib/ diff --git a/.travis.yml b/.travis.yml index adfc68c0d05..18cae39b04c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,13 @@ node_js: sudo: false after_script: - nyc report --reporter=text-lcov | coveralls +jobs: + include: + - stage: hosting functional test + node_js: '6' + before_script: ./scripts/decrypt-app-credentials.sh + script: ./scripts/test-hosting.sh + after_script: skip cache: directories: - node_modules diff --git a/scripts/creds-private.json.enc b/scripts/creds-private.json.enc new file mode 100644 index 00000000000..198b5dba2f9 Binary files /dev/null and b/scripts/creds-private.json.enc differ diff --git a/scripts/creds-public.json.enc b/scripts/creds-public.json.enc new file mode 100644 index 00000000000..ff5aa290480 Binary files /dev/null and b/scripts/creds-public.json.enc differ diff --git a/scripts/decrypt-app-credentials.sh b/scripts/decrypt-app-credentials.sh new file mode 100755 index 00000000000..ce6856f9271 --- /dev/null +++ b/scripts/decrypt-app-credentials.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -e + +openssl aes-256-cbc \ + -K $encrypted_830857fa25dd_key \ + -iv $encrypted_830857fa25dd_iv \ + -in scripts/creds-public.json.enc \ + -out scripts/creds-public.json \ + -d || true + +openssl aes-256-cbc \ + -K $encrypted_830857fa25dd_key \ + -iv $encrypted_830857fa25dd_iv \ + -in scripts/creds-private.json.enc \ + -out scripts/creds-private.json \ + -d || true + +test -f scripts/creds-public.json || test -f scripts/creds-private.json || (echo "No Credentials Decrypted" && false) diff --git a/scripts/test-hosting.sh b/scripts/test-hosting.sh new file mode 100755 index 00000000000..5b0c98a74b1 --- /dev/null +++ b/scripts/test-hosting.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash +set -e + +if [ "${TRAVIS}" != "true" ]; then + TRAVIS_COMMIT="localtesting" + TRAVIS_JOB_ID="$(echo $RANDOM)" + TRAVIS_REPO_SLUG="firebase/firebase-tools" +fi + +CWD="$(pwd)" +TARGET_FILE="${TRAVIS_COMMIT}-${TRAVIS_JOB_ID}.txt" + +GOOGLE_APPLICATION_CREDENTIALS="${CWD}/scripts/creds-private.json" +if [ "${TRAVIS_REPO_SLUG}" == "firebase/firebase-tools" ]; then + GOOGLE_APPLICATION_CREDENTIALS="${CWD}/scripts/creds-public.json" +fi +export GOOGLE_APPLICATION_CREDENTIALS + +echo "Running in ${CWD}" +echo "Running with node: $(which node)" +echo "Running with npm: $(which npm)" +echo "Running with Application Creds: ${GOOGLE_APPLICATION_CREDENTIALS}" + +echo "Target project: ${FBTOOLS_TARGET_PROJECT}" + +echo "Initalizing some variables..." +DATE="$(date)" +echo "Variables initalized..." + +echo "Creating temp directory..." +TEMP_DIR="$(mktemp -d)" +echo "Created temp directory: ${TEMP_DIR}" + +echo "Building and packaging firebase-tools..." +npm pack +FBT_PACKAGE="$(pwd)/$(ls *.tgz)" +echo "Built and packaged firebase-tools: ${FBT_PACKAGE}" + +echo "Installing firebase-tools..." +npm install --global "${FBT_PACKAGE}" +echo "Installed firebase-tools: $(which firebase)" + +echo "Initalizing temp directory..." +cd "${TEMP_DIR}" +cat > "firebase.json" <<- EOM +{ + "hosting": { + "public": "public", + "ignore": [ + "firebase.json", + "**/.*", + "**/node_modules/**" + ] + } +} +EOM +mkdir "public" +touch "public/${TARGET_FILE}" +echo "${DATE}" > "public/${TARGET_FILE}" +echo "Initalized temp directory." + +echo "Testing local serve..." +PORT=8685 +firebase serve --only hosting --project "${FBTOOLS_TARGET_PROJECT}" --port "${PORT}" & +PID="$!" +sleep 5 +VALUE="$(curl localhost:${PORT}/${TARGET_FILE})" +test "${DATE}" = "${VALUE}" || (echo "Expected ${VALUE} to equal ${DATE}." && false) +kill "$PID" +wait +echo "Tested local serve." + +echo "Testing hosting deployment..." +firebase deploy --only hosting --project "${FBTOOLS_TARGET_PROJECT}" +sleep 5 +VALUE="$(curl https://${FBTOOLS_TARGET_PROJECT}.firebaseapp.com/${TARGET_FILE})" +test "${DATE}" = "${VALUE}" || (echo "Expected ${VALUE} to equal ${DATE}." && false) +echo "Tested hosting deployment." diff --git a/src/api.js b/src/api.js index 40b01a72019..1957f841659 100644 --- a/src/api.js +++ b/src/api.js @@ -133,6 +133,9 @@ var api = { setAccessToken: function(token) { accessToken = token; }, + getScopes: function() { + return commandScopes; + }, setScopes: function(s) { commandScopes = _.uniq( _.flatten( diff --git a/src/requireAuth.js b/src/requireAuth.js index 731dea0ab82..ab3d4a7a456 100644 --- a/src/requireAuth.js +++ b/src/requireAuth.js @@ -33,9 +33,9 @@ function _autoAuth(options, authScopes) { }); } -module.exports = function(options, authScopes) { +module.exports = function(options) { api.setScopes([scopes.CLOUD_PLATFORM, scopes.FIREBASE_PLATFORM]); - options.authScopes = api.commandScopes; + options.authScopes = api.getScopes(); var tokens = configstore.get("tokens"); var user = configstore.get("user"); @@ -48,7 +48,7 @@ module.exports = function(options, authScopes) { } else if (user) { logger.debug("> authorizing via signed-in user"); } else { - return _autoAuth(options, authScopes); + return _autoAuth(options, options.authScopes); } tokenOpt = tokenOpt || process.env.FIREBASE_TOKEN;