Skip to content

Commit

Permalink
move [Discourse] url to query param (badges#4020)
Browse files Browse the repository at this point in the history
* refactor: switch Discourse url to query param

* tests: fix e2e test with Discourse example

* refactor(Discourse): change query param name to server

* tests: update e2e test with new discourse param name
  • Loading branch information
calebcartwright authored and repo-ranger[bot] committed Sep 16, 2019
1 parent db0ebf1 commit 51d864e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cypress/integration/main-page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Main page', function() {
expectBadgeExample(
'Discourse status',
'http://localhost:8080/badge/discourse-online-brightgreen',
'/discourse/:scheme/:host/status'
'/discourse/status?server=https%3A%2F%2Fmeta.discourse.org'
)
})

Expand Down
18 changes: 18 additions & 0 deletions services/discourse/discourse-redirect.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const { redirector } = require('..')

module.exports = [
redirector({
category: 'chat',
route: {
base: 'discourse',
pattern: ':protocol(http|https)/:hostAndPath(.+)/:metric',
},
transformPath: ({ metric }) => `/discourse/${metric}`,
transformQueryParams: ({ protocol, hostAndPath }) => ({
server: `${protocol}://${hostAndPath}`,
}),
dateAdded: new Date('2019-09-15'),
}),
]
69 changes: 69 additions & 0 deletions services/discourse/discourse-redirect.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict'

const { ServiceTester } = require('../tester')

const t = (module.exports = new ServiceTester({
id: 'DiscourseRedirect',
title: 'DiscourseRedirect',
pathPrefix: '/discourse',
}))

t.create('discourse status')
.get('/https/meta.discourse.org/status.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/discourse/status.svg?server=${encodeURIComponent(
'https://meta.discourse.org'
)}`
)

t.create('discourse topics')
.get('/https/meta.discourse.org/topics.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/discourse/topics.svg?server=${encodeURIComponent(
'https://meta.discourse.org'
)}`
)

t.create('discourse users')
.get('/https/meta.discourse.org/users.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/discourse/users.svg?server=${encodeURIComponent(
'https://meta.discourse.org'
)}`
)

t.create('discourse likes')
.get('/https/meta.discourse.org/likes.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/discourse/likes.svg?server=${encodeURIComponent(
'https://meta.discourse.org'
)}`
)

t.create('discourse posts')
.get('/https/meta.discourse.org/posts.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/discourse/posts.svg?server=${encodeURIComponent(
'https://meta.discourse.org'
)}`
)
35 changes: 19 additions & 16 deletions services/discourse/discourse.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const camelcase = require('camelcase')
const Joi = require('@hapi/joi')
const { metric } = require('../text-formatters')
const { nonNegativeInteger } = require('../validators')
const { nonNegativeInteger, optionalUrl } = require('../validators')
const { BaseJsonService } = require('..')

const schema = Joi.object({
Expand All @@ -13,6 +13,10 @@ const schema = Joi.object({
like_count: nonNegativeInteger,
}).required()

const queryParamSchema = Joi.object({
server: optionalUrl.required(),
}).required()

class DiscourseBase extends BaseJsonService {
static get category() {
return 'chat'
Expand All @@ -21,20 +25,19 @@ class DiscourseBase extends BaseJsonService {
static buildRoute(metric) {
return {
base: 'discourse',
// Do not base new services on this route pattern.
// See https://github.com/badges/shields/issues/3714
pattern: `:scheme(http|https)/:host/${metric}`,
pattern: metric,
queryParamSchema,
}
}

static get defaultBadgeData() {
return { label: 'discourse' }
}

async fetch({ scheme, host }) {
async fetch({ server }) {
return this._requestJson({
schema,
url: `${scheme}://${host}/site/statistics.json`,
url: `${server}/site/statistics.json`,
})
}
}
Expand All @@ -55,9 +58,9 @@ function DiscourseMetricIntegrationFactory({ metricName, property }) {
return [
{
title: `Discourse ${metricName}`,
namedParams: {
scheme: 'https',
host: 'meta.discourse.org',
namedParams: {},
queryParams: {
server: 'https://meta.discourse.org',
},
staticPreview: this.render({ stat: 100 }),
},
Expand All @@ -71,8 +74,8 @@ function DiscourseMetricIntegrationFactory({ metricName, property }) {
}
}

async handle({ scheme, host }) {
const data = await this.fetch({ scheme, host })
async handle(_routeParams, { server }) {
const data = await this.fetch({ server })
return this.constructor.render({ stat: data[property] })
}
}
Expand All @@ -87,9 +90,9 @@ class DiscourseStatus extends DiscourseBase {
return [
{
title: `Discourse status`,
namedParams: {
scheme: 'https',
host: 'meta.discourse.org',
namedParams: {},
queryParams: {
server: 'https://meta.discourse.org',
},
staticPreview: this.render(),
},
Expand All @@ -103,8 +106,8 @@ class DiscourseStatus extends DiscourseBase {
}
}

async handle({ scheme, host }) {
await this.fetch({ scheme, host })
async handle(_routeParams, { server }) {
await this.fetch({ server })
// if fetch() worked, the server is up
// if it failed, we'll show an error e.g: 'inaccessible'
return this.constructor.render()
Expand Down
29 changes: 12 additions & 17 deletions services/discourse/discourse.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const data = {
}

t.create('Topics')
.get('/https/meta.discourse.org/topics.json')
.get('/topics.json?server=https://meta.discourse.org')
.intercept(nock =>
nock('https://meta.discourse.org')
.get('/site/statistics.json')
Expand All @@ -35,7 +35,7 @@ t.create('Topics')
.expectBadge({ label: 'discourse', message: '23k topics' })

t.create('Posts')
.get('/https/meta.discourse.org/posts.json')
.get('/posts.json?server=https://meta.discourse.org')
.intercept(nock =>
nock('https://meta.discourse.org')
.get('/site/statistics.json')
Expand All @@ -44,7 +44,7 @@ t.create('Posts')
.expectBadge({ label: 'discourse', message: '338k posts' })

t.create('Users')
.get('/https/meta.discourse.org/users.json')
.get('/users.json?server=https://meta.discourse.org')
.intercept(nock =>
nock('https://meta.discourse.org')
.get('/site/statistics.json')
Expand All @@ -53,7 +53,7 @@ t.create('Users')
.expectBadge({ label: 'discourse', message: '31k users' })

t.create('Likes')
.get('/https/meta.discourse.org/likes.json')
.get('/likes.json?server=https://meta.discourse.org')
.intercept(nock =>
nock('https://meta.discourse.org')
.get('/site/statistics.json')
Expand All @@ -62,7 +62,7 @@ t.create('Likes')
.expectBadge({ label: 'discourse', message: '309k likes' })

t.create('Status')
.get('/https/meta.discourse.org/status.json')
.get('/status.json?server=https://meta.discourse.org')
.intercept(nock =>
nock('https://meta.discourse.org')
.get('/site/statistics.json')
Expand All @@ -71,21 +71,16 @@ t.create('Status')
.expectBadge({ label: 'discourse', message: 'online' })

t.create('Status with http (not https)')
.get('/http/meta.discourse.org/status.json')
.get('/status.json?server=http://meta.discourse.org')
.intercept(nock =>
nock('http://meta.discourse.org')
.get('/site/statistics.json')
.reply(200, data)
)
.expectBadge({ label: 'discourse', message: 'online' })

t.create('Status (offline)')
.get('/https/meta.discourse.org/status.json')
.networkOff()
.expectBadge({ label: 'discourse', message: 'inaccessible' })

t.create('Invalid Host')
.get('/https/some.host/status.json')
.get('/status.json?server=https://some.host')
.intercept(nock =>
nock('https://some.host')
.get('/site/statistics.json')
Expand All @@ -94,7 +89,7 @@ t.create('Invalid Host')
.expectBadge({ label: 'discourse', message: 'not found' })

t.create('Topics')
.get('/https/meta.discourse.org/topics.json')
.get('/topics.json?server=https://meta.discourse.org')
.expectBadge({
label: 'discourse',
message: Joi.string().regex(
Expand All @@ -103,7 +98,7 @@ t.create('Topics')
})

t.create('Posts')
.get('/https/meta.discourse.org/posts.json')
.get('/posts.json?server=https://meta.discourse.org')
.expectBadge({
label: 'discourse',
message: Joi.string().regex(
Expand All @@ -112,7 +107,7 @@ t.create('Posts')
})

t.create('Users')
.get('/https/meta.discourse.org/users.json')
.get('/users.json?server=https://meta.discourse.org')
.expectBadge({
label: 'discourse',
message: Joi.string().regex(
Expand All @@ -121,7 +116,7 @@ t.create('Users')
})

t.create('Likes')
.get('/https/meta.discourse.org/likes.json')
.get('/likes.json?server=https://meta.discourse.org')
.expectBadge({
label: 'discourse',
message: Joi.string().regex(
Expand All @@ -130,5 +125,5 @@ t.create('Likes')
})

t.create('Status')
.get('/https/meta.discourse.org/status.json')
.get('/status.json?server=https://meta.discourse.org')
.expectBadge({ label: 'discourse', message: 'online' })

0 comments on commit 51d864e

Please sign in to comment.