Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/scratch-frame/src/ScratchEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const ScratchEditor = ({
locale,
apiUrl,
accessToken: initialAccessToken,
projectLocale,
}) => {
const [accessToken, setAccessToken] = useState(initialAccessToken);

Expand Down Expand Up @@ -89,6 +90,10 @@ const ScratchEditor = ({
basePath={`${import.meta.env.REACT_APP_SCRATCH_FRAME_URL}/scratch-gui/`}
onStorageInit={(storage) => {
scratchFetchApiRef.current = storage.scratchFetch;
scratchFetchApiRef.current.setMetadata(
"X-Project-Locale",
projectLocale,
);
if (accessToken) {
scratchFetchApiRef.current.setMetadata("Authorization", accessToken);
}
Expand Down
26 changes: 26 additions & 0 deletions apps/scratch-frame/src/ScratchEditor.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@ describe("ScratchEditor", () => {
);
});

test("sends the project locale with scratch requests after storage init", () => {
render(
<ScratchEditor
projectId="project-123"
locale="fr"
apiUrl="https://api.example.com"
accessToken="token-123"
projectLocale="fr-FR"
/>,
);

const scratchGuiProps = mockWrappedScratchGui.mock.calls[0][0];
const scratchStorage = {
scratchFetch: {
setMetadata: vi.fn(),
},
};

scratchGuiProps.onStorageInit(scratchStorage);

expect(scratchStorage.scratchFetch.setMetadata).toHaveBeenCalledWith(
"X-Project-Locale",
"fr-FR",
);
});

test("updates scratchFetch metadata when scratch-gui-update-token message is received", () => {
render(
<ScratchEditor
Expand Down
1 change: 1 addition & 0 deletions apps/scratch-frame/src/scratch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ if (!projectId) {
locale={locale}
apiUrl={apiUrl}
accessToken={accessToken}
projectLocale={raspberryPiLocale}
/>
</>,
);
Expand Down
4 changes: 3 additions & 1 deletion apps/scratch-frame/src/scratch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ describe("scratch handshake retries", () => {
);

await loadScratchModule();
expect(mountScratchEditor().locale).toBe(expected);
const scratchEditorProps = mountScratchEditor();
expect(scratchEditorProps.locale).toBe(expected);
expect(scratchEditorProps.projectLocale).toBe(locale);
},
);

Expand Down
14 changes: 13 additions & 1 deletion cypress/e2e/spec-scratch.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
const origin = "http://localhost:3011/web-component.html";
const scratchFrameOrigin = Cypress.env("REACT_APP_SCRATCH_FRAME_URL");
const authKey = "oidc.user:https://auth-v1.raspberrypi.org:editor-api";
const scratchProjectsApiMatcher = "**/api/scratch/projects/**";
const user = {
access_token: "dummy-access-token",
profile: {
Expand Down Expand Up @@ -107,6 +108,18 @@ describe("Scratch locale", () => {
});
});

it("sets a header with the project locale", () => {
cy.intercept("GET", scratchProjectsApiMatcher).as("scratchProjectRequest");

cy.visit(scratchProjectURL({ locale: "fr-FR" }));

cy.wait("@scratchProjectRequest")
.its("request.headers")
.then((headers) => {
expect(headers["x-project-locale"]).to.equal("fr-FR");
});
});

it("falls back to English when Scratch does not support the locale", () => {
cy.visit(scratchProjectURL({ locale: "vls-BE" }));

Expand Down Expand Up @@ -220,7 +233,6 @@ describe("Scratch save integration", () => {
});

describe("Scratch Authorization header", () => {
const scratchProjectsApiMatcher = "**/api/scratch/projects/**";
const remixApiMatcher = "**/api/projects/*/remix";

it("includes Authorization header when authKey and access token are present in localStorage", () => {
Expand Down
Loading