forked from confused-Techie/atom-backend
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from pulsar-edit/add-filter-support
Add filter support to `/api/packages/search`
- Loading branch information
Showing
5 changed files
with
149 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module.exports = { | ||
schema: { | ||
name: "filter", | ||
in: "query", | ||
schema: { | ||
type: "string", | ||
enum: [ | ||
"package", | ||
"theme" | ||
], | ||
default: "pacakge" | ||
}, | ||
required: false, | ||
allowEmptyValue: false, | ||
example: "package", | ||
description: "Deprecated method to display packages or themes. Use `/api/themes/search` or `/api/packages/search` instead." | ||
}, | ||
logic: (req) => { | ||
const def = "package"; | ||
const valid = [ "theme", "package" ]; | ||
|
||
const prov = req.query.filter; | ||
|
||
if (typeof prov !== "string") { | ||
return def; | ||
} | ||
|
||
if (!valid.includes(prov)) { | ||
return def; | ||
} | ||
|
||
return prov; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
const endpoint = require("../../src/controllers/getPackagesSearch.js"); | ||
const database = require("../../src/database.js"); | ||
const context = require("../../src/context.js"); | ||
|
||
describe("Behaves as expected", () => { | ||
|
||
beforeAll(async () => { | ||
await database.insertNewPackage({ | ||
name: "get-packages-search-theme-test", | ||
repository: { | ||
url: "https://github.com/getPackagesSearch/get-packages-search-theme-test", | ||
type: "git" | ||
}, | ||
owner: "getPackagesSearch", | ||
creation_method: "Test Package", | ||
releases: { | ||
latest: "1.0.0" | ||
}, | ||
readme: "This is a readme!", | ||
metadata: { | ||
name: "get-packages-search-theme-test", | ||
theme: "syntax" | ||
}, | ||
versions: { | ||
"1.0.0": { | ||
dist: { | ||
tarball: "download-url", | ||
sha: "1234" | ||
}, | ||
name: "get-packages-search-theme-test" | ||
} | ||
} | ||
}); | ||
|
||
await database.insertNewPackage({ | ||
name: "get-packages-search-test", | ||
repository: { | ||
url: "https://github.com/getPackagesSearch/get-packages-search-test", | ||
type: "git" | ||
}, | ||
owner: "getPackagesSearch", | ||
creation_method: "Test Package", | ||
releases: { | ||
latest: "1.0.0" | ||
}, | ||
readme: "This is a readme!", | ||
metadata: { | ||
name: "get-packages-search-test" | ||
}, | ||
versions: { | ||
"1.0.0": { | ||
dist: { | ||
tarball: "download-url", | ||
sha: "1234" | ||
}, | ||
name: "get-packages-search-test" | ||
} | ||
} | ||
}); | ||
|
||
}); | ||
|
||
afterAll(async () => { | ||
await database.removePackageByName("get-packages-search-theme-test"); | ||
await database.removePackageByName("get-packages-search-test"); | ||
}); | ||
|
||
test("Successfully searches", async () => { | ||
let sso = await endpoint.logic({ | ||
sort: "downloads", | ||
page: 1, | ||
direction: "desc", | ||
query: "get packages search", | ||
filter: "package" | ||
}, context); | ||
|
||
expect(sso.ok).toBe(true); | ||
expect(sso.content.length).toBe(2); | ||
expect(sso.content[0].name).toBe("get-packages-search-test"); | ||
expect(sso.content[1].name).toBe("get-packages-search-theme-test"); | ||
expect(sso).toMatchEndpointSuccessObject(endpoint); | ||
}); | ||
|
||
test("Successfully searches and filters by theme", async () => { | ||
let sso = await endpoint.logic({ | ||
sort: "downloads", | ||
page: 1, | ||
direction: "desc", | ||
query: "get packages search", | ||
filter: "theme" | ||
}, context); | ||
|
||
expect(sso.ok).toBe(true); | ||
expect(sso.content.length).toBe(1); | ||
expect(sso.content[0].name).toBe("get-packages-search-theme-test"); | ||
expect(sso).toMatchEndpointSuccessObject(endpoint); | ||
}); | ||
}); |