Skip to content

Commit

Permalink
refactor - buckets variable is moved to base class, also updated test…
Browse files Browse the repository at this point in the history
…er with createTestService.
  • Loading branch information
MohanKumarAmbati committed Oct 24, 2024
1 parent 9b68c04 commit e7e40bb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
4 changes: 4 additions & 0 deletions services/scoop/scoop-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const bucketsSchema = Joi.object()
.required()

export class ScoopBase extends ConditionalGithubAuthV3Service {
// The buckets file (https://github.com/lukesampson/scoop/blob/master/buckets.json) changes very rarely.
// Cache it for the lifetime of the current Node.js process.
buckets = null

async fetch({ app, schema }, queryParams) {
if (!this.buckets) {
this.buckets = await fetchJsonFromRepo(this, {
Expand Down
4 changes: 0 additions & 4 deletions services/scoop/scoop-license.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ const queryParamSchema = Joi.object({
})

export default class ScoopLicense extends ScoopBase {
// The buckets file (https://github.com/lukesampson/scoop/blob/master/buckets.json) changes very rarely.
// Cache it for the lifetime of the current Node.js process.
buckets = null

static category = 'license'

static route = {
Expand Down
23 changes: 12 additions & 11 deletions services/scoop/scoop-license.tester.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
import { createServiceTester } from '../tester.js'

export const t = await createServiceTester()

t.create('License (valid) - with nested response')
.get('/l/ngrok.json')
.get('/ngrok.json')
.expectBadge({
label: 'license',
message: 'Shareware',
})

t.create('License (valid) - with string response')
.get('/l/nvs.json')
.get('/nvs.json')
.expectBadge({
label: 'license',
message: 'MIT',
})

t.create('License (invalid)').get('/l/not-a-real-app.json').expectBadge({
t.create('License (invalid)').get('/not-a-real-app.json').expectBadge({
label: 'license',
message: 'not-a-real-app not found in bucket "main"',
})

t.create('License (valid custom bucket)')
.get('/l/atom.json?bucket=extras')
.get('/atom.json?bucket=extras')
.expectBadge({
label: 'license',
message: 'MIT',
})

t.create('license (not found in custom bucket)')
.get('/l/not-a-real-app.json?bucket=extras')
.get('/not-a-real-app.json?bucket=extras')
.expectBadge({
label: 'license',
message: 'not-a-real-app not found in bucket "extras"',
})

t.create('license (wrong bucket)')
.get('/l/not-a-real-app.json?bucket=not-a-real-bucket')
.get('/not-a-real-app.json?bucket=not-a-real-bucket')
.expectBadge({
label: 'license',
message: 'bucket "not-a-real-bucket" not found',
Expand All @@ -47,7 +48,7 @@ const validBucketUrl = encodeURIComponent(
)

t.create('license (valid bucket url)')
.get(`/l/sfsu.json?bucket=${validBucketUrl}`)
.get(`/sfsu.json?bucket=${validBucketUrl}`)
.expectBadge({
label: 'license',
message: 'Apache-2.0',
Expand All @@ -58,14 +59,14 @@ const validBucketUrlTrailingSlash = encodeURIComponent(
)

t.create('license (valid bucket url)')
.get(`/l/sfsu.json?bucket=${validBucketUrlTrailingSlash}`)
.get(`/sfsu.json?bucket=${validBucketUrlTrailingSlash}`)
.expectBadge({
label: 'license',
message: 'Apache-2.0',
})

t.create('license (not found in custom bucket)')
.get(`/l/not-a-real-app.json?bucket=${validBucketUrl}`)
.get(`/not-a-real-app.json?bucket=${validBucketUrl}`)
.expectBadge({
label: 'license',
message: `not-a-real-app not found in bucket "${decodeURIComponent(validBucketUrl)}"`,
Expand All @@ -74,7 +75,7 @@ t.create('license (not found in custom bucket)')
const nonGithubUrl = encodeURIComponent('https://example.com/')

t.create('license (non-github url)')
.get(`/l/not-a-real-app.json?bucket=${nonGithubUrl}`)
.get(`/not-a-real-app.json?bucket=${nonGithubUrl}`)
.expectBadge({
label: 'license',
message: `bucket "${decodeURIComponent(nonGithubUrl)}" not found`,
Expand All @@ -83,7 +84,7 @@ t.create('license (non-github url)')
const nonBucketRepo = encodeURIComponent('https://github.com/jewlexx/sfsu')

t.create('version (non-bucket repo)')
.get(`/l/sfsu.json?bucket=${nonBucketRepo}`)
.get(`/sfsu.json?bucket=${nonBucketRepo}`)
.expectBadge({
label: 'license',
// !!! Important note here
Expand Down
4 changes: 0 additions & 4 deletions services/scoop/scoop-version.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ const queryParamSchema = Joi.object({
})

export default class ScoopVersion extends ScoopBase {
// The buckets file (https://github.com/lukesampson/scoop/blob/master/buckets.json) changes very rarely.
// Cache it for the lifetime of the current Node.js process.
buckets = null

static category = 'version'

static route = {
Expand Down

0 comments on commit e7e40bb

Please sign in to comment.