Skip to content

Commit

Permalink
refactor: settings v2 (BREAKING CHANGE) (#282)
Browse files Browse the repository at this point in the history
* feat: add new file services for Settings

To modify our settings page, we need to introduce new file services
for files which we read and write settings data from:
- _config.yml
- _data/footer.yml
- _data/navigation.yml
- index.md (homepage)

The NavYmlService already exists so we don't need to create a file
service for _data/navigation.yml.

* feat: add new Settings router

This commit adds the V2 SettingsRouter class with an implementation
of the readSettingsPage route. The update route will be implemented
in a subsequent commit.

* refactor: extract settings files retrieval to method

This commit extracts the settings file retrieval logic into a separate
method on the SettingsRouter class. This is because the file retrieval
logic is needed for both the read and update methods of the SettingsRouter.

* refactor: update settings page

This commit refactors the update route for Settings. The refactor
simplifies the code structure by eliminating the use of an
intermediary "update" object which tracks which files need to be
updated. Using the new file services allows us to eliminate the
need for formatting the content before posting to GitHub.

* chore: import v2 settings router

* refactor: add settings update schema

* test: add sample data for tests

* refactor: readSettingsPage response schema

This commit refactors the readSettingsPage response to mirror the
UpdateSettingsRequestSchema.

This commit also refactors the readSettingsPage route function to
use util functions to extract the required fields for the response.

* chore: add URL attribute back to settings response

* test: readSettingsPage

This commit completes the test for the readSettingsPage route
function.

It also updates the fixtures to have the expected response from
each of the page services.

* chore: update settings request schema, fix logic error in updateSettingPage

This commit updates the settings request schema to allow empty string
values, and assigning required values correctly.

It also fixes a logic error in checking whether or not to update
the homepage when making a change to the title.

* refactor: simplify footer update code

* test: updateSettingsPage

This commit completes the unit tests for the updateSettingsPage route
function.

* test: HomepagePageService unit tests

This commit adds the unit tests for the "read" and "update" methods
of the HomepagePageService. It also adds the necessary fixtures for
the homepage.

* test: ConfigYmlService unit tests

This commit adds the unit tests for the "read" and "update" methods
of the ConfigYmlService. It also adds the necessary fixtures for
the config file.

* test: FooterYmlService unit tests

This commit adds the unit tests for the "read" and "update" methods
of the FooterYmlService. It also adds the necessary fixtures for
the footer file.

* test: NavYmlService unit tests

This commit adds the unit tests for the "read" and "update" methods
of the NavYmlService. It also adds the necessary fixtures for
the navigation file.

* chore: add v2 settings route to auth middleware

* chore: add description to Settings v2

In PR 280, we added a description field to the Settings endpoints
(see #280). This
commit adds this field to the Settings V2 router as well.

* refactor: create new configService category

This commit refactors the new Settings route to use a new
SettingsService so that business logic in the routes layer is
simplified - the details of HOW the Settings files are updated
should not be exposed on the route layer.

In doing so, we created a new configServices directory to store
the SettingsService, since it does not exist as a file service.

* chore: update config fixtures with description field

* test: add tests for SettingsService

This commit adds the necessary tests for the SettingsService,
most of which were copied over and refactored from the route tests.
Another change made was to use the toHaveBeenLastCalledWith method
instead of the toHaveBeenCalledWith method, since it more accurately
reflects the change we want to test.

* test: refactor Settings route tests

This commit refactors the Settings route tests so that we only mock the
SettingsService class and not the underlying file services that we had
used before.

This commit also refactors the existing SettingsService utility methods
to be static methods so that we can more easily test our code.

* chore: typo

* Fix: wait for expected method to fully resolve

This commit modifies how we wait for a function to be completed.

Previously, we would use await expect(func), but this caused some issues where not all method calls within the function were awaited properly - by changing this to await expect(func).resolves.not.toThrow(), this problem is resolved.

* Fix: rebase errors

* Fix: update schema to allow telegram and tiktok

* fix: rebase errors

* Refactor: change format of settings object

* Fix: update test

Co-authored-by: Alexander Lee <alexander@open.gov.sg>
  • Loading branch information
kwajiehao and alexanderleegs authored Nov 22, 2021
1 parent 535b9dc commit ad1f194
Show file tree
Hide file tree
Showing 22 changed files with 1,561 additions and 38 deletions.
139 changes: 139 additions & 0 deletions fixtures/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
const rawConfigContent = `title: abcdefg
description: Brief site description here
url: https://abc.gov.sg
favicon: /images/isomer-logo.svg
colors:
primary-color: "#d10404"
secondary-color: "#09b709"
media-colors:
- title: media-color-one
color: "#162938"
- title: media-color-two
color: "#8a3ce0"
- title: media-color-three
color: "#18808d"
- title: media-color-four
color: "#22b0c3"
- title: media-color-five
color: "#0b3033"
permalink: none
baseurl: ""
exclude:
- travis-script.js
- .travis.yml
- README.md
- package.json
- package-lock.json
- node_modules
- vendor/bundle/
- vendor/cache/
- vendor/gems/
- vendor/ruby/
- Gemfile
- Gemfile.lock
include:
- _redirects
defaults:
- scope:
path: ""
values:
layout: page
custom_css_path: /misc/custom.css
custom_print_css_path: /assets/css/print.css
paginate: 12
remote_theme: isomerpages/isomerpages-template@next-gen
safe: false
plugins:
- jekyll-feed
- jekyll-assets
- jekyll-paginate
- jekyll-sitemap
- jekyll-remote-theme
staging: https://e2e-test-repo-staging.netlify.app
prod: https://e2e-test-repo-prod.netlify.app
resources_name: resources
is_government: false
shareicon: /images/isomer-logo.svg
facebook-pixel: "true"
google_analytics: UA-39345131-33
linkedin-insights: "12345"
`

const configContent = {
title: "abcdefg",
description: "Brief site description here",
url: "https://abc.gov.sg",
favicon: "/images/isomer-logo.svg",
colors: {
"primary-color": "#d10404",
"secondary-color": "#09b709",
"media-colors": [
{ title: "media-color-one", color: "#162938" },
{ title: "media-color-two", color: "#8a3ce0" },
{ title: "media-color-three", color: "#18808d" },
{ title: "media-color-four", color: "#22b0c3" },
{ title: "media-color-five", color: "#0b3033" },
],
},
permalink: "none",
baseurl: "",
exclude: [
"travis-script.js",
".travis.yml",
"README.md",
"package.json",
"package-lock.json",
"node_modules",
"vendor/bundle/",
"vendor/cache/",
"vendor/gems/",
"vendor/ruby/",
"Gemfile",
"Gemfile.lock",
],
include: ["_redirects"],
defaults: [{ scope: { path: "" }, values: { layout: "page" } }],
custom_css_path: "/misc/custom.css",
custom_print_css_path: "/assets/css/print.css",
paginate: 12,
remote_theme: "isomerpages/isomerpages-template@next-gen",
safe: false,
plugins: [
"jekyll-feed",
"jekyll-assets",
"jekyll-paginate",
"jekyll-sitemap",
"jekyll-remote-theme",
],
staging: "https://e2e-test-repo-staging.netlify.app",
prod: "https://e2e-test-repo-prod.netlify.app",
resources_name: "resources",
is_government: false,
shareicon: "/images/isomer-logo.svg",
"facebook-pixel": "true",
google_analytics: "UA-39345131-33",
"linkedin-insights": "12345",
}

const configSha = "configsha"

const configResponse = {
url: configContent.url,
title: configContent.title,
description: configContent.description,
favicon: configContent.favicon,
shareicon: configContent.shareicon,
is_government: configContent.is_government,
facebook_pixel: configContent["facebook-pixel"],
google_analytics: configContent.google_analytics,
linkedin_insights: configContent["linkedin-insights"],
resources_name: configContent.resources_name,
colors: configContent.colors,
}

module.exports = {
configContent,
configSha,
configResponse,
rawConfigContent,
}
38 changes: 38 additions & 0 deletions fixtures/footer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const rawFooterContent = `show_reach: true
feedback: www.feedbacktest.com/hihi
faq: /faqpagetest/hihi
social_media:
facebook: https://www.facebook.com/testfbnow
twitter: https://www.twitter.com/testtwitternow
youtube: https://www.youtube.com/testyoutubenow
instagram: https://www.instagram.com/testinstanow
linkedin: https://www.linkedin.com/company/testagencynow
contact_us: astast
`

const footerContent = {
show_reach: true,
feedback: "www.feedbacktest.com/hihi",
faq: "/faqpagetest/hihi",
social_media: {
facebook: "https://www.facebook.com/testfbnow",
twitter: "https://www.twitter.com/testtwitternow",
youtube: "https://www.youtube.com/testyoutubenow",
instagram: "https://www.instagram.com/testinstanow",
linkedin: "https://www.linkedin.com/company/testagencynow",
},
contact_us: "astast",
}

const footerSha = "footerSha"

const footerResponse = {
...footerContent,
}

module.exports = {
footerContent,
footerSha,
footerResponse,
rawFooterContent,
}
89 changes: 89 additions & 0 deletions fixtures/homepage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const rawHomepageContent = `---
layout: homepage
title: abcdefg
description: Brief site description here
image: /images/isomer-logo.svg
permalink: /
notification: Here's a notification bar you can use!
sections:
- hero:
title: Hero titlZZZZ
subtitle: Hero subtitle
background: /images/hero-banner.png
button: Contact Us
url: /contact-us/
key_highlights:
- title: Highlight A
description: Important highlight A is important
url: https://google.com
- title: Highlight B
description: Important highlight B is equally important
url: https://gmail.com
- title: Page A
description: Page A is important too
url: /privacy/
- infobar:
title: Infobar title
subtitle: Subtitle
description: About a sentence worth of description here
button: Button text
url: /faq/
---
`

const homepageContent = {
frontMatter: {
layout: "homepage",
title: "abcdefg",
description: "Brief site description here",
image: "/images/isomer-logo.svg",
permalink: "/",
notification: "Here's a notification bar you can use!",
sections: [
{
hero: {
title: "Hero titlZZZZ",
subtitle: "Hero subtitle",
background: "/images/hero-banner.png",
button: "Contact Us",
url: "/contact-us/",
key_highlights: [
{
title: "Highlight A",
description: "Important highlight A is important",
url: "https://google.com",
},
{
title: "Highlight B",
description: "Important highlight B is equally important",
url: "https://gmail.com",
},
{
title: "Page A",
description: "Page A is important too",
url: "/privacy/",
},
],
},
},
{
infobar: {
title: "Infobar title",
subtitle: "Subtitle",
description: "About a sentence worth of description here",
button: "Button text",
url: "/faq/",
},
},
],
},
pageBody: "\n",
}

const homepageSha = "homepageSha"

module.exports = {
homepageContent,
homepageSha,
rawHomepageContent,
}
37 changes: 37 additions & 0 deletions fixtures/navigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const rawNavigationContent = `logo: /images/favicon-isomer.ico
links:
- title: Contact Us
url: /contact-us/
- title: Test
url: /Test/
- title: Resources
resource_room: true
- title: Test Folder Title No Pages
collection: test-folder-title-no-pages
`

const navigationContent = {
logo: "/images/favicon-isomer.ico",
links: [
{ title: "Contact Us", url: "/contact-us/" },
{ title: "Test", url: "/Test/" },
{ title: "Resources", resource_room: true },
{
title: "Test Folder Title No Pages",
collection: "test-folder-title-no-pages",
},
],
}

const navigationSha = "navigationSha"

const navigationResponse = {
logo: navigationContent.logo,
}

module.exports = {
navigationContent,
navigationSha,
navigationResponse,
rawNavigationContent,
}
4 changes: 4 additions & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ auth.post(
auth.get("/v1/sites/:siteName/settings", verifyJwt)
auth.post("/v1/sites/:siteName/settings", verifyJwt)

// New settings
auth.get("/v2/sites/:siteName/settings", verifyJwt)
auth.post("/v2/sites/:siteName/settings", verifyJwt)

// Navigation
auth.get("/v1/sites/:siteName/navigation", verifyJwt)
auth.post("/v1/sites/:siteName/navigation", verifyJwt)
Expand Down
Loading

0 comments on commit ad1f194

Please sign in to comment.