Skip to content

Commit

Permalink
fix(routeselector/resourceroom): remove accidental comments + fix typo (
Browse files Browse the repository at this point in the history
#1859)

* fix(routeselector/resourceroom): remove accidental comments + fix typo

* fix(routeselector): validate tou

* fix(validators): add new regex

* test(routeselector): remove old check + fix tests
  • Loading branch information
seaerchin authored Mar 21, 2024
1 parent 76bc4ac commit f3dc16c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/__tests__/RouteSelector.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ describe("Tests for RouteSelector", () => {
render(
<MemoryRouter
initialEntries={[
"/sites/site-name/resourceRoom/my-resource/resourceCategory/my-category/editPage/my-file",
"/sites/site-name/resourceRoom/my-resource/resourceCategory/my-category/editPage/my-file.md",
]}
>
<LoggedInContextProvider>
Expand Down Expand Up @@ -518,7 +518,7 @@ describe("Tests for RouteSelector", () => {
test("Should render EditPage page for site site-name if logged in", () => {
// Act
render(
<MemoryRouter initialEntries={["/sites/site-name/editPage/my-file"]}>
<MemoryRouter initialEntries={["/sites/site-name/editPage/my-file.md"]}>
<LoggedInContextProvider>
<RouteSelector />
</LoggedInContextProvider>
Expand Down Expand Up @@ -656,7 +656,7 @@ describe("Tests for RouteSelector", () => {
render(
<MemoryRouter
initialEntries={[
"/sites/site-name/folders/my-folder/editPage/my-file",
"/sites/site-name/folders/my-folder/editPage/my-file.md",
]}
>
<LoggedInContextProvider>
Expand Down Expand Up @@ -693,7 +693,7 @@ describe("Tests for RouteSelector", () => {
render(
<MemoryRouter
initialEntries={[
"/sites/site-name/folders/my-folder/subfolders/my%20subfolder/editPage/my-file",
"/sites/site-name/folders/my-folder/subfolders/my%20subfolder/editPage/my-file.md",
]}
>
<LoggedInContextProvider>
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/ResourceRoom/ResourceRoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ const ResourceRoomContent = ({
>
<FormLabel>Resource room title</FormLabel>
<Input
placeholder="New resource oom name"
placeholder="New resource room name"
{...register("newDirectoryName", {
required:
"Please ensure that you have entered a resource room name!",
Expand Down
28 changes: 7 additions & 21 deletions src/routing/RouteSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { Workspace } from "layouts/Workspace"
import { ProtectedRouteWithProps } from "routing/ProtectedRouteWithProps"
import RedirectIfLoggedInRoute from "routing/RedirectIfLoggedInRoute"

import { specialCharactersRegexTest } from "utils"
import { ALLOWED_CHARACTERS_REGEX } from "utils"

import {
ApprovedReviewRedirect,
Expand Down Expand Up @@ -69,21 +69,6 @@ export const RouteSelector = () => {
exact
path={[
"/sites/:siteName([a-zA-Z0-9-]+)/resourceRoom/:resourceRoomName([a-zA-Z0-9-]+)/resourceCategory/:resourceCategoryName([a-zA-Z0-9-]+)/editPage/:fileName",
]}
component={injectApprovalRedirect(EditPage)}
validate={{
fileName: (value) => {
const encodedName = value.split(".").slice(0, -1).join(".")
return !specialCharactersRegexTest.test(encodedName)
},
}}
/>

<ProtectedRouteWithProps
exact
path={[
// Collection is correct
// Subcollection disallows a few special characters
"/sites/:siteName([a-zA-Z0-9-]+)/folders/:collectionName([a-zA-Z0-9-]+)/subfolders/:subCollectionName/editPage/:fileName",
"/sites/:siteName([a-zA-Z0-9-]+)/folders/:collectionName([a-zA-Z0-9-]+)/editPage/:fileName",
"/sites/:siteName([a-zA-Z0-9-]+)/editPage/:fileName",
Expand All @@ -92,10 +77,11 @@ export const RouteSelector = () => {
validate={{
fileName: (value) => {
const encodedName = value.split(".").slice(0, -1).join(".")
return !specialCharactersRegexTest.test(encodedName)
const decodedName = decodeURIComponent(encodedName)
return ALLOWED_CHARACTERS_REGEX.test(decodedName)
},
subCollectionName: (value) => {
return !specialCharactersRegexTest.test(value)
return ALLOWED_CHARACTERS_REGEX.test(decodeURIComponent(value))
},
}}
/>
Expand All @@ -107,7 +93,7 @@ export const RouteSelector = () => {
]}
validate={{
subCollectionName: (value) => {
return !specialCharactersRegexTest.test(value)
return ALLOWED_CHARACTERS_REGEX.test(decodeURIComponent(value))
},
}}
>
Expand All @@ -131,8 +117,8 @@ export const RouteSelector = () => {
// NOTE: This value is prepended with either `files|images`
// and nested directories are separated by `/` as well.
const decodedValues = decodeURIComponent(value).split("/")
return decodedValues.every(
(val) => !specialCharactersRegexTest.test(val)
return decodedValues.every((val) =>
ALLOWED_CHARACTERS_REGEX.test(val)
)
},
}}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export const fileNameExtensionRegexTest = /^[a-zA-z]{3,4}$/
export const RESOURCE_CATEGORY_REGEX = "^([a-zA-Z0-9]*[- ]?)+$"
export const slugifyLowerFalseRegexTest = /^([a-zA-Z0-9]+-)*[a-zA-Z0-9]+$/
export const resourceCategoryRegexTest = RegExp(RESOURCE_CATEGORY_REGEX)
// NOTE: This is a negation of the specialCharactersRegex
// and also allows for `-`.
// This is because of migration of pre-cms sites from github
// over to cms allows for dashes in the file/folder name.
export const ALLOWED_CHARACTERS_REGEX = /[^~%^*_+./\\`;~{}[\]"<>]/
export const specialCharactersRegexTest = /[~%^*_+\-./\\`;~{}[\]"<>]/
export const jekyllFirstCharacterRegexTest = /^[._#~]/
export const mediaSpecialCharactersRegexTest = /[~%^?*+#./\\`;~{}[\]"<>]/
Expand Down

0 comments on commit f3dc16c

Please sign in to comment.