Skip to content

Commit

Permalink
migrate [MozillaObservatory] to new API (#10402)
Browse files Browse the repository at this point in the history
* migrate [MozillaObservatory] to new API

* remove timeouts

* prettier
  • Loading branch information
chris48s authored Jul 26, 2024
1 parent be542e1 commit da0fd70
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 368 deletions.
72 changes: 18 additions & 54 deletions services/mozilla-observatory/mozilla-observatory.service.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,21 @@
import Joi from 'joi'
import { BaseJsonService, queryParam, pathParam } from '../index.js'
import { BaseJsonService, pathParam } from '../index.js'

const schema = Joi.object({
state: Joi.string()
.valid('ABORTED', 'FAILED', 'FINISHED', 'PENDING', 'STARTING', 'RUNNING')
.required(),
grade: Joi.alternatives()
.conditional('state', {
is: 'FINISHED',
then: Joi.string().regex(/^[ABCDEF][+-]?$/),
otherwise: Joi.valid(null),
})
.required(),
score: Joi.alternatives()
.conditional('state', {
is: 'FINISHED',
then: Joi.number().integer().min(0).max(200),
otherwise: Joi.valid(null),
})
.required(),
}).required()

const queryParamSchema = Joi.object({
publish: Joi.equal(''),
scan: Joi.object({
grade: Joi.string()
.regex(/^[ABCDEF][+-]?$/)
.required(),
score: Joi.number().integer().min(0).max(200).required(),
}).required(),
}).required()

const description = `
The [Mozilla HTTP Observatory](https://observatory.mozilla.org)
The [Mozilla HTTP Observatory](https://developer.mozilla.org/en-US/observatory)
is a set of security tools to analyze your website
and inform you if you are utilizing the many available methods to secure it.
`

const publishDescription = `
By default the scan result is hidden from the public result list.
You can activate the publication of the scan result
by setting the \`publish\` parameter.
The badge returns a cached site result if the site has been scanned anytime in the previous 24 hours.
If you need to force invalidating the cache,
you can to do it manually through the [Mozilla Observatory Website](https://observatory.mozilla.org)
`

export default class MozillaObservatory extends BaseJsonService {
// TODO: Once created, change to a more appropriate category,
// see https://github.com/badges/shields/pull/2926#issuecomment-460777017
Expand All @@ -49,7 +24,6 @@ export default class MozillaObservatory extends BaseJsonService {
static route = {
base: 'mozilla-observatory',
pattern: ':format(grade|grade-score)/:host',
queryParamSchema,
}

static openApi = {
Expand All @@ -67,12 +41,6 @@ export default class MozillaObservatory extends BaseJsonService {
name: 'host',
example: 'github.com',
}),
queryParam({
name: 'publish',
schema: { type: 'boolean' },
example: null,
description: publishDescription,
}),
],
},
},
Expand All @@ -82,13 +50,7 @@ export default class MozillaObservatory extends BaseJsonService {
label: 'observatory',
}

static render({ format, state, grade, score }) {
if (state !== 'FINISHED') {
return {
message: state.toLowerCase(),
color: 'lightgrey',
}
}
static render({ format, grade, score }) {
const letter = grade[0].toLowerCase()
const colorMap = {
a: 'brightgreen',
Expand All @@ -104,20 +66,22 @@ export default class MozillaObservatory extends BaseJsonService {
}
}

async fetch({ host, publish }) {
async fetch({ host }) {
return this._requestJson({
schema,
url: 'https://http-observatory.security.mozilla.org/api/v1/analyze',
url: 'https://observatory-api.mdn.mozilla.net/api/v2/analyze',
options: {
method: 'POST',
searchParams: { host },
form: { hidden: !publish },
},
})
}

async handle({ format, host }, { publish }) {
const { state, grade, score } = await this.fetch({ host, publish })
return this.constructor.render({ format, state, grade, score })
async handle({ format, host }) {
const { scan } = await this.fetch({ host })
return this.constructor.render({
format,
grade: scan.grade,
score: scan.score,
})
}
}
93 changes: 93 additions & 0 deletions services/mozilla-observatory/mozilla-observatory.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { test, given } from 'sazerac'
import MozillaObservatory from './mozilla-observatory.service.js'

describe('MozillaObservatory', function () {
test(MozillaObservatory.render, () => {
given({ format: 'grade', grade: 'A' }).expect({
message: 'A',
color: 'brightgreen',
})
given({ format: 'grade', grade: 'A+' }).expect({
message: 'A+',
color: 'brightgreen',
})
given({ format: 'grade', grade: 'A-' }).expect({
message: 'A-',
color: 'brightgreen',
})

given({ format: 'grade', grade: 'B' }).expect({
message: 'B',
color: 'green',
})
given({ format: 'grade', grade: 'B+' }).expect({
message: 'B+',
color: 'green',
})
given({ format: 'grade', grade: 'B-' }).expect({
message: 'B-',
color: 'green',
})

given({ format: 'grade', grade: 'C' }).expect({
message: 'C',
color: 'yellow',
})
given({ format: 'grade', grade: 'C+' }).expect({
message: 'C+',
color: 'yellow',
})
given({ format: 'grade', grade: 'C-' }).expect({
message: 'C-',
color: 'yellow',
})

given({ format: 'grade', grade: 'D' }).expect({
message: 'D',
color: 'orange',
})
given({ format: 'grade', grade: 'D+' }).expect({
message: 'D+',
color: 'orange',
})
given({ format: 'grade', grade: 'D-' }).expect({
message: 'D-',
color: 'orange',
})

given({ format: 'grade', grade: 'E' }).expect({
message: 'E',
color: 'orange',
})
given({ format: 'grade', grade: 'E+' }).expect({
message: 'E+',
color: 'orange',
})
given({ format: 'grade', grade: 'E-' }).expect({
message: 'E-',
color: 'orange',
})

given({ format: 'grade', grade: 'F' }).expect({
message: 'F',
color: 'red',
})
given({ format: 'grade', grade: 'F+' }).expect({
message: 'F+',
color: 'red',
})
given({ format: 'grade', grade: 'F-' }).expect({
message: 'F-',
color: 'red',
})

given({
format: 'grade-score',
grade: 'A',
score: '115',
}).expect({
message: 'A (115/100)',
color: 'brightgreen',
})
})
})
Loading

0 comments on commit da0fd70

Please sign in to comment.