Merge branch 'main' into feature/dashboard

This commit is contained in:
Keith Solomon
2025-05-25 13:36:32 -05:00
10 changed files with 1057 additions and 266 deletions

11
app.js
View File

@@ -4,8 +4,10 @@ const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
const apiRouter = require('./routes/api');
const sitesRouter = require('./routes/sites');
const app = express();
@@ -20,8 +22,13 @@ app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
/**
* Register routers for different routes
*/
app.use('/', indexRouter);
app.use('/api', apiRouter);
app.use('/users', usersRouter);
app.use('/sites', sitesRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {

8
auth.js Normal file
View File

@@ -0,0 +1,8 @@
const { createClient } = require('@supabase/supabase-js');
const supabase = createClient(
'https://rwulmnzzmieuosakijoe.supabase.co',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InJ3dWxtbnp6bWlldW9zYWtpam9lIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDgwNTQ4NDEsImV4cCI6MjA2MzYzMDg0MX0.ZEBJ6v0-u77z1cqRJvA8WOcGRxd8fcN0vqeYJNQgd_U'
);
module.exports.supabase = supabase;

17
helpers/axe-test.js Normal file
View File

@@ -0,0 +1,17 @@
const base = require('@playwright/test');
const AxeBuilder = require('@axe-core/playwright').default;
// Extend base test by providing "makeAxeBuilder"
//
// This new "test" can be used in multiple test files, and each of them will get
// a consistently configured AxeBuilder instance.
exports.test = base.test.extend({
makeAxeBuilder: async ({ page }, use) => {
const makeAxeBuilder = () => new AxeBuilder({ page })
.withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa'])
.exclude('#commonly-reused-element-with-known-issue');
await use(makeAxeBuilder);
}
});
exports.expect = base.expect;

69
models/SiteModel.js Normal file
View File

@@ -0,0 +1,69 @@
/**
* Model for managing sites table in the database.
*/
const { supabase } = require('../auth');
class SiteModel {
static tableName = 'sites';
/**
* Inserts a new site into the database.
*
* @param {string} name - The name of the site.
* @param {string} domainName - The domain name of the site.
*
* @returns {Promise<Object>} - The result of the insert operation.
*/
async insert(name, domainName) {
const { error } = await supabase.from(siteModel.tableName).insert({
name: name,
domain_name: domainName,
});
if (error) {
console.error('Error inserting site:', error);
throw error;
}
return data;
}
/**
* Retrieves all sites from the database.
*
* @returns {Promise<Array>} - An array of site objects.
*/
async getAll() {
const { data, error } = await supabase.from(SiteModel.tableName).select('*');
if (error) {
console.error('Error fetching sites:', error);
throw error;
}
return data;
}
/**
* Retrieves a site by its ID.
*
* @param {number} id - The ID of the site.
*
* @returns {Promise<Object>} - The site object.
*/
async getById(id) {
const { data, error } = await supabase.from(SiteModel.tableName).select('*').eq('id', id).single();
if (error) {
console.error('Error fetching site by ID:', error);
throw error;
}
return data;
}
}
module.exports = SiteModel;

996
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -9,12 +9,17 @@
"build:css": "tailwindcss -i ./styles/base.css -o ./public/stylesheets/style.css --minify"
},
"dependencies": {
"@axe-core/playwright": "^4.10.1",
"@playwright/test": "^1.52.0",
"@supabase/supabase-js": "^2.49.8",
"axe-playwright": "^2.1.0",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"ejs": "^3.1.10",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1"
"morgan": "~1.9.1",
"jsdom": "^26.1.0"
},
"devDependencies": {
"@tailwindcss/cli": "^4.1.7",

27
routes/api.js Normal file
View File

@@ -0,0 +1,27 @@
// import router from express
var express = require('express');
const PlaywrightService = require('../services/PlaywrightService');
var router = express.Router();
/**
* GET run accessibility test on domain
* Only works for homepage
* // TODO: Crawl site map and queue all pages
*/
router.get('/test/accessibility/:domain', function(req, res) {
req.setTimeout(5000000000000);
const domain = req.params.domain; // Get the domain from the request parameters
const playwrightService = new PlaywrightService(domain);
// Call the runAccessibilityTest method
playwrightService.getAccessibilityResults().then(results => {
// Send the results as a JSON response
res.json(results);
}).catch(err => {
// Handle any errors that occur during the test
res.status(500).json({ error: err.message });
});
});
module.exports = router;

View File

@@ -1,9 +1,13 @@
var express = require('express');
var router = express.Router();
const SiteModel = require('../models/SiteModel');
const sitesModel = new SiteModel();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Playwright Dashboard' });
router.get('/', async function(req, res, next) {
const sites = await sitesModel.getAll();
console.log('Sites:', sites);
res.render('index', { title: 'Express', sites: sites });
});
module.exports = router;

70
routes/sites.js Normal file
View File

@@ -0,0 +1,70 @@
const express = require('express');
const router = express.Router();
const SiteModel = require('../models/SiteModel');
const siteModel = new SiteModel();
/**
* GET sites listing.
*
* Returns a list of all sites in JSON format.
*/
router.get('/', async function(req, res, next) {
const sites = await siteModel.getAll();
res.json(sites);
});
/**
* GET site by ID.
*
* Returns a specific site by its ID in JSON format.
*/
router.get('/:id', async function(req, res, next) {
const siteId = req.params.id;
const site = await siteModel.getById(siteId);
if (!site) {
return res.status(404).send('Site not found');
} else {
res.json(site);
}
});
/**
* POST create a new site.
*
* Creates a new site with the provided domain name.
*
* // TODO: Implement validation for domain name format
* // TODO: Implement error handling for duplicate domains
* // TODO: Ability to add additional site properties (e.g., name, description)
*/
router.post('/add/:domain', function(req, res, next) {
const domain = req.params.domain;
const newSite = siteModel.insert(domain);
if (!newSite) {
return res.status(400).send('Error creating site');
} else {
res.status(201).json(newSite);
}
});
/**
* PUT update an existing site.
*
* Updates an existing site with the provided ID and new data.
*/
// TODO: Implement update functionality
/**
* DELETE remove a site by ID.
*/
// TODO: Implement delete functionality
module.exports = router;

View File

@@ -0,0 +1,110 @@
// import playwright dependencies
const { chromium } = require('playwright');
const { injectAxe, checkA11y } = require('axe-playwright');
const jsdom = require("jsdom");
/**
* PlaywrightService class
*
* This class is used to interact with the Playwright library
*/
class PlaywrightService {
#domain; // domain of the site to be tested
// constructor
constructor(domain) {
this.#domain = domain;
}
// get sitemap url for the this domain
sitemapUrl() {
return `${this.rootUrl()}/sitemap.xml`;
}
// get the url of the site to be tested
rootUrl() {
return `https://${this.#domain}`;
}
/**
* Get the list of urls to be tested by querying
* the sitemap and returning the list of urls
*
* @param {string} url - The URL of the sitemap
*
* @returns {Array} - The list of urls to be tested
*/
async getUrlList() {
const browser = await chromium.launch();
const page = await browser.newPage();
let urls = [];
try {
await page.goto(this.sitemapUrl());
const content = await page.content();
const dom = new jsdom.JSDOM(content);
const sitemapUrls = dom.window.document.querySelectorAll('a[href]');
urls = Array.from(sitemapUrls).map(link => link.href);
console.log('Sitemap URLs:', urls);
} catch (error) {
console.error('Error fetching sitemap:', error);
} finally {
await browser.close();
}
return urls;
}
/**
* Loops through the list of urls and runs the
* accessibility test on each url
*
* @returns {Array} - The list of results from the accessibility test
*/
async getAccessibilityResults() {
const urls = await this.getUrlList();
let results = [];
while (urls.length > 0) {
const url = urls.pop();
console.log('Running accessibility test on:', url);
const result = await PlaywrightService.#runAccessibilityTest(url);
results.push([url, result]);
}
return results;
}
/**
* Run accessibility test on given url
*
* @param {string} url - The URL of the page to test
*
* @returns {Array} - The list of results from the accessibility test
*/
static async #runAccessibilityTest(url) {
const browser = await chromium.launch();
const page = await browser.newPage();
let results = [];
try {
await page.goto(url); // TODO: Retry if this times out
await injectAxe(page);
results = await page.evaluate(async () => {
return await window.axe.run();
});
} catch(error) {
console.error('Error running accessibility test:', error);
} finally {
await browser.close();
}
return results?.violations || 'No violations found';
}
}
module.exports = PlaywrightService;