Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 112 additions & 1 deletion __fixtures__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,10 +920,121 @@ const sampleBulkImportFileContent = [{
is_subscribed: true
}]

// Sample project data for testing validateProjectData
const sampleProjectData = {
project: {
id: 1,
name: 'Test Project',
description: 'A test project',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z'
},
checks: [
{
id: 1,
checklist_id: 1,
description: 'Test check',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z'
}
],
results: [
{
id: 1,
project_id: 1,
name: 'Test result',
score: 8.5,
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z'
}
],
tasks: [
{
id: 1,
project_id: 1,
description: 'Test task',
implementation_status: 'pending',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z'
}
],
alerts: [
{
id: 1,
project_id: 1,
title: 'Test alert',
description: 'This is a test alert',
severity: 'medium',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z'
}
],
githubOrgs: [
{
id: 1,
login: 'test-org',
name: 'Test Organization',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z'
}
],
githubRepos: [
{
id: 1,
name: 'test-repo',
full_name: 'test-org/test-repo',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z'
}
],
ossfScorecardResults: [
{
id: 1,
repository_id: 1,
score: 8.5,
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z'
}
]
}

// Sample index data for testing validateIndexData
const sampleIndexData = {
projects: [
{
id: 1,
name: 'Test Project',
description: 'A test project',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z'
}
],
checklists: [
{
id: 1,
name: 'Test Checklist',
type: 'security',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z'
}
],
checks: [
{
id: 1,
checklist_id: 1,
description: 'Test check',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z'
}
]
}

module.exports = {
sampleGithubOrg,
sampleGithubListOrgRepos,
sampleGithubRepository,
sampleOSSFScorecardResult,
sampleBulkImportFileContent
sampleBulkImportFileContent,
sampleProjectData,
sampleIndexData
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const request = require('supertest')
const { generateStaticReports } = require('../src/reports')
const serverModule = require('../src/httpServer')
const { generateStaticReports } = require('../../src/reports')
const serverModule = require('../../src/httpServer')
let server
let serverStop
let app

// Mocks
jest.mock('../src/reports', () => ({
jest.mock('../../src/reports', () => ({
generateStaticReports: jest.fn()
}))

Expand All @@ -27,7 +27,7 @@ beforeEach(() => {
jest.clearAllMocks()
})

describe('HTTP Server API', () => {
describe('HTTP Server API V1', () => {
describe('GET /api/v1/__health', () => {
test('should return status ok', async () => {
const response = await app.get('/api/v1/__health')
Expand Down
69 changes: 69 additions & 0 deletions __tests__/httpServer/website.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const request = require('supertest')
const knexInit = require('knex')
const serverModule = require('../../src/httpServer')
const { getConfig } = require('../../src/config')
const { resetDatabase, initializeStore } = require('../../__utils__')

const { dbSettings } = getConfig('test')

let server
let serverStop
let app
let knex
let addProject
let testProjectId

beforeAll(async () => {
// Initialize database
knex = knexInit(dbSettings);
({ addProject } = initializeStore(knex))

// Reset database and add test project
await resetDatabase(knex)
const testProject = await addProject({ name: 'Test Project' })
testProjectId = testProject.id

// Initialize server asynchronously
const serverInstance = serverModule()
server = await serverInstance.start()
serverStop = serverInstance.stop
app = request(server)
})

afterAll(async () => {
// Cleanup after all tests
await serverStop?.()
await resetDatabase(knex)
await knex.destroy()
})

describe('HTTP Server WEBSITE', () => {
describe('GET /', () => {
it('should render index page', async () => {
const response = await app.get('/')
expect(response.status).toBe(200)
expect(response.header['content-type']).toMatch(/text\/html/)
})
})

describe('GET /projects/:id', () => {
it('should render project page for valid project ID', async () => {
const response = await app.get(`/projects/${testProjectId}`)
expect(response.status).toBe(200)
expect(response.header['content-type']).toMatch(/text\/html/)
})

it('should render notFound page for invalid project ID format', async () => {
const response = await app.get('/projects/invalid')
expect(response.status).toBe(404)
expect(response.header['content-type']).toMatch(/text\/html/)
})

it('should render notFound page for non-existent project ID', async () => {
const nonExistentId = 9999
const response = await app.get(`/projects/${nonExistentId}`)
expect(response.status).toBe(404)
expect(response.header['content-type']).toMatch(/text\/html/)
})
})
})
47 changes: 47 additions & 0 deletions __tests__/reports.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { internalLinkBuilder } = require('../src/reports')

describe('internalLinkBuilder', () => {
describe('static mode', () => {
const staticLinkBuilder = internalLinkBuilder('static')

test('should handle empty reference', () => {
expect(staticLinkBuilder('', null)).toBe('index.html')
})

test('should remove leading slash in static mode', () => {
expect(staticLinkBuilder('/assets/favicon.ico', null)).toBe('assets/favicon.ico')
})

test('should handle project references in static mode', () => {
const project = { name: 'testproject', id: 123 }
expect(staticLinkBuilder('', project)).toBe('testproject.html')
})

test('should prioritize project reference over path', () => {
const project = { name: 'testproject', id: 123 }
expect(staticLinkBuilder('/some/path', project)).toBe('testproject.html')
})
})

describe('server mode', () => {
const serverLinkBuilder = internalLinkBuilder('server')

test('should handle empty reference', () => {
expect(serverLinkBuilder('', null)).toBe('index.html')
})

test('should preserve leading slash in server mode', () => {
expect(serverLinkBuilder('/assets/favicon.ico', null)).toBe('/assets/favicon.ico')
})

test('should handle project references in server mode', () => {
const project = { name: 'testproject', id: 123 }
expect(serverLinkBuilder('', project)).toBe('/projects/123')
})

test('should prioritize project reference over path', () => {
const project = { name: 'testproject', id: 123 }
expect(serverLinkBuilder('/some/path', project)).toBe('/projects/123')
})
})
})
Loading
Loading