Skip to content

Commit

Permalink
Merge pull request #1671 from weather-gov/jt/outside-testing
Browse files Browse the repository at this point in the history
"outside" testing proof of concept
  • Loading branch information
jamestranovich-noaa authored Sep 4, 2024
2 parents 3366b9c + bb2d0bf commit f7fcb53
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 25 deletions.
34 changes: 27 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,25 @@ export-content: ## Export all content to web/scs-export
rm web/scs-export/*.zip
docker compose exec drupal drush content:export node scs-export --all-content

# set a docker image variable so that we do not have to duplicate makefile rules for testing.
drupal_image = "drupal"

import-config: ## Import the Drupal configuration from the config directory into your site
docker compose exec drupal drush config:import -y
docker compose exec drupal drush twig:debug on
docker compose exec drupal drush state:set disable_rendered_output_cache_bins 1
docker compose exec ${drupal_image} drush config:import -y
docker compose exec ${drupal_image} drush twig:debug on
docker compose exec ${drupal_image} drush state:set disable_rendered_output_cache_bins 1

import-content: web/scs-export/* ## Import content from web/scs-export
for file in $^; do \
file="$${file#*/}"; \
docker compose exec drupal drush content:import "$$file"; \
docker compose exec ${drupal_image} drush content:import "$$file"; \
done

install-site: install-site-config import-content ## Install a minimal Drupal site using the configuration in the config directory and exported content
install-site-config:
docker compose exec drupal drush site:install minimal --existing-config --account-pass=root -y
docker compose exec drupal drush twig:debug on
docker compose exec drupal drush state:set disable_rendered_output_cache_bins 1
docker compose exec ${drupal_image} drush site:install minimal --existing-config --account-pass=root -y
docker compose exec ${drupal_image} drush twig:debug on
docker compose exec ${drupal_image} drush state:set disable_rendered_output_cache_bins 1

log: ## Tail the log for the Drupal container
docker compose logs --follow drupal
Expand Down Expand Up @@ -107,6 +110,9 @@ build-sprites: # Build sprites
load-spatial: # Load spatial data into the database
docker compose run --rm spatial node load-shapefiles.js

load-spatial-test: # Load spatial data into the test database
docker compose -f docker-compose.test.yml --profile test run --rm spatial-test node load-shapefiles.js

### Testing
a11y: accessibility-test
accessibility-test: ## Run accessibility tests (alias a11y)
Expand All @@ -132,6 +138,20 @@ u: unit-test
unit-test: ## Run PHP unit tests
docker compose exec drupal phpunit --group unit

start-test-environment: destroy-test-environment
docker compose -f docker-compose.test.yml --profile test up -d

destroy-test-environment:
docker compose -f docker-compose.test.yml --profile test down

setup-outside-vars:
$(eval drupal_image="drupal-test")

ot: outside-test
outside-test: setup-outside-vars start-test-environment pause install-site load-spatial-test ## Run a separate weather.gov instance for testing
./tests/playwright/outside/setup.sh
npx playwright test outside/*

### Linting
js-lint: ## Run eslint on our Javascript
npm run js-lint
Expand Down
43 changes: 43 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
services:
drupal-test:
extends:
file: ./docker-compose.yml
service: drupal
ports: !override
- 9080:80
links: !override
- database-test
- api-proxy-test
environment: # we want to extend, not override
API_URL: http://api-proxy-test:9081
DRUPAL_DB_HOST: database-test
profiles: !override ["test"]

database-test:
extends:
file: ./docker-compose.yml
service: database
ports: !override
- 4306:3306
profiles: !override ["test"]

api-proxy-test:
extends:
file: ./docker-compose.yml
service: api-proxy
ports: !override
- 9081:8081
profiles: !override ["test"]

spatial-test:
extends:
file: ./docker-compose.yml
service: spatial
links: !override
- database-test
environment:
DB_HOST: database-test
profiles: !override ["test"]

networks:
weather.gov:
34 changes: 17 additions & 17 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
services:
database:
# 8.0.35 in prod
image: mysql:8.0
image: mysql:8.0 # 8.0.35 in prod
networks:
- weather.gov
environment:
Expand Down Expand Up @@ -89,43 +88,44 @@ services:
image: 18f-zscaler-plantum:jetty
ports:
- 8180:8080
profiles: ["utilities"]
profiles: ["utility"]

utility-node:
build:
context: .
dockerfile: Dockerfile.utility-node
profiles: ["utility"]
networks:
- weather.gov
links:
- database
volumes:
- ./:/app
- /app/node_modules
profiles: ["utility"]

uswds:
build:
context: ./
dockerfile: Dockerfile.uswds
profiles: ["utility"]
volumes:
- ./web/themes/new_weather_theme/:/app
- /app/node_modules
profiles: ["utility"]

spatial:
build:
context: ./spatial-data
dockerfile: Dockerfile.spatial
image: 18f-spatial-node:20
profiles: ["spatial"]
networks:
- weather.gov
links:
- database
volumes:
- ./spatial-data/:/app
- /app/node_modules
build:
context: ./spatial-data
dockerfile: Dockerfile.spatial
image: 18f-spatial-node:20
networks:
- weather.gov
links:
- database
environment:
DB_HOST: database
volumes:
- ./spatial-data/:/app
- /app/node_modules

networks:
weather.gov:
2 changes: 1 addition & 1 deletion spatial-data/lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports.openDatabase = async () => {
user: args[0] ?? "drupal",
password: args[1] ?? "drupal",
database: args[2] ?? "weathergov",
host: args[3] ?? "database",
host: args[3] ?? (process.env.DB_HOST ?? "database"),
port: args[4] ?? 3306,
};

Expand Down
55 changes: 55 additions & 0 deletions tests/playwright/outside/api.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { test, expect } = require("@playwright/test");

const { describe } = test;

const API_ENDPOINT = "http://localhost:9080/jsonapi";

describe("API tests", () => {
test("users without credentials cannot get an API listing", async ({
request,
}) => {
const response = await request.get(API_ENDPOINT);
expect(response.ok()).not.toBeTruthy();
const json = await response.json();
expect(json).toHaveProperty("errors");
expect(json).toMatchObject({
errors: [
{
detail: "No authentication credentials provided.",
title: "Unauthorized",
status: "401",
},
],
});
});

test("anonymous users cannot get an API listing", async ({ request }) => {
const response = await request.get(API_ENDPOINT, {
headers: {
Authorization: `Basic ${btoa("foo:foo")}`,
},
});
expect(response.ok()).not.toBeTruthy();
const json = await response.json();
expect(json).toHaveProperty("errors");
expect(json).toMatchObject({
errors: [
{
title: "Forbidden",
status: "403",
},
],
});
});

test("uploader can see the API listing", async ({ request }) => {
const response = await request.get(API_ENDPOINT, {
headers: {
Authorization: `Basic ${btoa("uploader:testpass")}`,
},
});
expect(response.ok()).toBeTruthy();
const json = await response.json();
expect(json).not.toHaveProperty("errors");
});
});
6 changes: 6 additions & 0 deletions tests/playwright/outside/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

set -euxo pipefail

docker compose exec drupal-test drush user:create uploader --mail='testuser@noaa.com' --password='testpass'
docker compose exec drupal-test drush user:role:add 'uploader' uploader

0 comments on commit f7fcb53

Please sign in to comment.