Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix insertion of access tokens, when swapping renderer #1021

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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
strategy:
fail-fast: false
matrix:
browser: [chrome, firefox]
browser: [chrome]

runs-on: ubuntu-22.04
steps:
Expand Down
37 changes: 36 additions & 1 deletion cypress/e2e/modals.cy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { MaputnikDriver } from "./maputnik-driver";
import tokens from "../../src/config/tokens.json" with {type: "json"};

describe("modals", () => {
const { beforeAndAfter, when, get, then } = new MaputnikDriver();
const { beforeAndAfter, when, get, given, then } = new MaputnikDriver();
beforeAndAfter();

beforeEach(() => {
Expand Down Expand Up @@ -235,6 +236,40 @@ describe("modals", () => {
metadata: { "maputnik:renderer": "ol" },
});
});



it("inlcude API key when change renderer", () => {

when.click("modal:settings.close-modal")
when.click("nav:open");

get.elementByAttribute('aria-label', "MapTiler Basic").should('exist').click();

when.click("nav:settings");

when.select("modal:settings.maputnik:renderer", "mlgljs");
then(get.inputValue("modal:settings.maputnik:renderer")).shouldEqual(
"mlgljs"
);

when.select("modal:settings.maputnik:renderer", "ol");
then(get.inputValue("modal:settings.maputnik:renderer")).shouldEqual(
"ol"
);

given.intercept("https://api.maptiler.com/tiles/v3-openmaptiles/tiles.json?key=*", "tileRequest", "GET");

when.select("modal:settings.maputnik:renderer", "mlgljs");
then(get.inputValue("modal:settings.maputnik:renderer")).shouldEqual(
"mlgljs"
);

when.waitForResponse("tileRequest").its("request").its("url").should("include", `https://api.maptiler.com/tiles/v3-openmaptiles/tiles.json?key=${tokens.openmaptiles}`);
when.waitForResponse("tileRequest").its("request").its("url").should("include", `https://api.maptiler.com/tiles/v3-openmaptiles/tiles.json?key=${tokens.openmaptiles}`);
when.waitForResponse("tileRequest").its("request").its("url").should("include", `https://api.maptiler.com/tiles/v3-openmaptiles/tiles.json?key=${tokens.openmaptiles}`);
});

});

describe("sources", () => {
Expand Down
21 changes: 21 additions & 0 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ export default class App extends React.Component<any, AppState> {
[property]: value
}
}

this.onStyleChanged(changedStyle)
}

Expand All @@ -374,6 +375,24 @@ export default class App extends React.Component<any, AppState> {
...opts,
};

// For the style object, find the urls that has "{key}" and insert the correct API keys
// Without this, going from e.g. MapTiler to OpenLayers and back will lose the maptlier key.

if (newStyle.glyphs && typeof newStyle.glyphs === 'string') {
newStyle.glyphs = setFetchAccessToken(newStyle.glyphs, newStyle);
}

if (newStyle.sprite && typeof newStyle.sprite === 'string') {
newStyle.sprite = setFetchAccessToken(newStyle.sprite, newStyle);
}

for (const [_sourceId, source] of Object.entries(newStyle.sources)) {
if (source && 'url' in source && typeof source.url === 'string') {
source.url = setFetchAccessToken(source.url, newStyle);
}
}


if (opts.initialLoad) {
this.getInitialStateFromUrl(newStyle);
}
Expand Down Expand Up @@ -737,6 +756,7 @@ export default class App extends React.Component<any, AppState> {
onLayerSelect={this.onLayerSelect}
/>
} else {

mapElement = <MapMaplibreGl {...mapProps}
onChange={this.onMapChange}
options={this.state.maplibreGlDebugOptions}
Expand Down Expand Up @@ -790,6 +810,7 @@ export default class App extends React.Component<any, AppState> {
getInitialStateFromUrl = (mapStyle: StyleSpecification) => {
const url = new URL(location.href);
const modalParam = url.searchParams.get("modal");

if (modalParam && modalParam !== "") {
const modals = modalParam.split(",");
const modalObj: {[key: string]: boolean} = {};
Expand Down
Loading