Add fake user switch to mimick a logged in user; fixes #432

This commit is contained in:
pietervdvn 2021-07-16 02:06:33 +02:00
parent abd7db100d
commit 3bcd255311
5 changed files with 182 additions and 129 deletions

View file

@ -20,6 +20,34 @@ the URL-parameters are stated in the part between the `?` and the `#`. There are
Finally, the URL-hash is the part after the `#`. It is `node/1234` in this case. Finally, the URL-hash is the part after the `#`. It is `node/1234` in this case.
backend
---------
The OSM backend to use - can be used to redirect mapcomplete to the testing backend when using 'osm-test' The default value is _osm_
test
------
If true, 'dryrun' mode is activated. The app will behave as normal, except that changes to OSM will be printed onto the console instead of actually uploaded to osm.org The default value is _false_
layout
--------
The layout to load into MapComplete The default value is __
userlayout
------------
If not 'false', a custom (non-official) theme is loaded. This custom layout can be done in multiple ways:
- The hash of the URL contains a base64-encoded .json-file containing the theme definition
- The hash of the URL contains a lz-compressed .json-file, as generated by the custom theme generator
- The parameter itself is an URL, in which case that URL will be downloaded. It should point to a .json of a theme The default value is _false_
layer-control-toggle layer-control-toggle
---------------------- ----------------------
@ -35,19 +63,19 @@ Finally, the URL-hash is the part after the `#`. It is `node/1234` in this case.
z z
--- ---
The initial/current zoom level The default value is _0_ The initial/current zoom level The default value is _14_
lat lat
----- -----
The initial/current latitude The default value is _0_ The initial/current latitude The default value is _51.2095_
lon lon
----- -----
The initial/current longitude of the app The default value is _0_ The initial/current longitude of the app The default value is _3.2228_
fs-userbadge fs-userbadge
@ -110,10 +138,16 @@ Finally, the URL-hash is the part after the `#`. It is `node/1234` in this case.
Always show all questions The default value is _false_ Always show all questions The default value is _false_
test fs-export
------ -----------
If true, 'dryrun' mode is activated. The app will behave as normal, except that changes to OSM will be printed onto the console instead of actually uploaded to osm.org The default value is _false_ If set, enables the 'download'-button to download everything as geojson The default value is _false_
fake-user
-----------
If true, 'dryrun' mode is activated and a fake user account is loaded The default value is _false_
debug debug
@ -122,12 +156,6 @@ Finally, the URL-hash is the part after the `#`. It is `node/1234` in this case.
If true, shows some extra debugging help such as all the available tags on every object The default value is _false_ If true, shows some extra debugging help such as all the available tags on every object The default value is _false_
backend
---------
The OSM backend to use - can be used to redirect mapcomplete to the testing backend when using 'osm-test' The default value is _osm_
custom-css custom-css
------------ ------------
@ -140,6 +168,10 @@ Finally, the URL-hash is the part after the `#`. It is `node/1234` in this case.
The id of the background layer to start with The default value is _osm_ The id of the background layer to start with The default value is _osm_
oauth_token
-------------
Used to complete the login No default value set
layer-<layer-id> layer-<layer-id>
------------------ ------------------

View file

@ -47,6 +47,7 @@ export class OsmConnection {
public auth; public auth;
public userDetails: UIEventSource<UserDetails>; public userDetails: UIEventSource<UserDetails>;
public isLoggedIn: UIEventSource<boolean> public isLoggedIn: UIEventSource<boolean>
private fakeUser: boolean;
_dryRun: boolean; _dryRun: boolean;
public preferencesHandler: OsmPreferences; public preferencesHandler: OsmPreferences;
public changesetHandler: ChangesetHandler; public changesetHandler: ChangesetHandler;
@ -59,12 +60,15 @@ export class OsmConnection {
url: string url: string
}; };
constructor(dryRun: boolean, oauth_token: UIEventSource<string>, constructor(dryRun: boolean,
fakeUser: boolean,
oauth_token: UIEventSource<string>,
// Used to keep multiple changesets open and to write to the correct changeset // Used to keep multiple changesets open and to write to the correct changeset
layoutName: string, layoutName: string,
singlePage: boolean = true, singlePage: boolean = true,
osmConfiguration: "osm" | "osm-test" = 'osm' osmConfiguration: "osm" | "osm-test" = 'osm'
) { ) {
this.fakeUser = fakeUser;
this._singlePage = singlePage; this._singlePage = singlePage;
this._oauth_config = OsmConnection.oauth_configs[osmConfiguration] ?? OsmConnection.oauth_configs.osm; this._oauth_config = OsmConnection.oauth_configs[osmConfiguration] ?? OsmConnection.oauth_configs.osm;
console.debug("Using backend", this._oauth_config.url) console.debug("Using backend", this._oauth_config.url)
@ -72,7 +76,15 @@ export class OsmConnection {
this._iframeMode = Utils.runningFromConsole ? false : window !== window.top; this._iframeMode = Utils.runningFromConsole ? false : window !== window.top;
this.userDetails = new UIEventSource<UserDetails>(new UserDetails(this._oauth_config.url), "userDetails"); this.userDetails = new UIEventSource<UserDetails>(new UserDetails(this._oauth_config.url), "userDetails");
this.userDetails.data.dryRun = dryRun; this.userDetails.data.dryRun = dryRun || fakeUser;
if(fakeUser){
const ud = this.userDetails.data;
ud.csCount = 5678
ud.loggedIn= true;
ud.unreadMessages = 0
ud.name = "Fake user"
ud.totalMessages = 42;
}
const self =this; const self =this;
this.isLoggedIn = this.userDetails.map(user => user.loggedIn).addCallback(isLoggedIn => { this.isLoggedIn = this.userDetails.map(user => user.loggedIn).addCallback(isLoggedIn => {
if(self.userDetails.data.loggedIn == false && isLoggedIn == true){ if(self.userDetails.data.loggedIn == false && isLoggedIn == true){
@ -138,6 +150,10 @@ export class OsmConnection {
} }
public AttemptLogin() { public AttemptLogin() {
if(this.fakeUser){
console.log("AttemptLogin called, but ignored as fakeUser is set")
return;
}
const self = this; const self = this;
console.log("Trying to log in..."); console.log("Trying to log in...");
this.updateAuthObject(); this.updateAuthObject();

View file

@ -97,7 +97,7 @@ export default class State {
public readonly featureSwitchShowAllQuestions: UIEventSource<boolean>; public readonly featureSwitchShowAllQuestions: UIEventSource<boolean>;
public readonly featureSwitchApiURL: UIEventSource<string>; public readonly featureSwitchApiURL: UIEventSource<string>;
public readonly featureSwitchEnableExport: UIEventSource<boolean>; public readonly featureSwitchEnableExport: UIEventSource<boolean>;
public readonly featureSwitchFakeUser: UIEventSource<boolean>;
public readonly featurePipeline: FeaturePipeline; public readonly featurePipeline: FeaturePipeline;
@ -212,6 +212,10 @@ export default class State {
"If true, 'dryrun' mode is activated. The app will behave as normal, except that changes to OSM will be printed onto the console instead of actually uploaded to osm.org") "If true, 'dryrun' mode is activated. The app will behave as normal, except that changes to OSM will be printed onto the console instead of actually uploaded to osm.org")
.map(str => str === "true", [], b => "" + b); .map(str => str === "true", [], b => "" + b);
this.featureSwitchFakeUser = QueryParameters.GetQueryParameter("fake-user", "false",
"If true, 'dryrun' mode is activated and a fake user account is loaded")
.map(str => str === "true", [], b => "" + b);
this.featureSwitchIsDebugging = QueryParameters.GetQueryParameter("debug", "false", this.featureSwitchIsDebugging = QueryParameters.GetQueryParameter("debug", "false",
"If true, shows some extra debugging help such as all the available tags on every object") "If true, shows some extra debugging help such as all the available tags on every object")
.map(str => str === "true", [], b => "" + b) .map(str => str === "true", [], b => "" + b)
@ -241,6 +245,7 @@ export default class State {
this.osmConnection = new OsmConnection( this.osmConnection = new OsmConnection(
this.featureSwitchIsTesting.data, this.featureSwitchIsTesting.data,
this.featureSwitchFakeUser.data,
QueryParameters.GetQueryParameter("oauth_token", undefined, QueryParameters.GetQueryParameter("oauth_token", undefined,
"Used to complete the login"), "Used to complete the login"),
layoutToUse?.id, layoutToUse?.id,

View file

@ -12,7 +12,7 @@ import BaseUIElement from "./UI/BaseUIElement";
import Table from "./UI/Base/Table"; import Table from "./UI/Base/Table";
const connection = new OsmConnection(false, new UIEventSource<string>(undefined), ""); const connection = new OsmConnection(false, false, new UIEventSource<string>(undefined), "");
let rendered = false; let rendered = false;

View file

@ -15,7 +15,7 @@ export default class OsmConnectionSpec extends T {
super("OsmConnectionSpec-test", [ super("OsmConnectionSpec-test", [
["login on dev", ["login on dev",
() => { () => {
const osmConn = new OsmConnection(false, const osmConn = new OsmConnection(false,false,
new UIEventSource<string>(undefined), new UIEventSource<string>(undefined),
"Unit test", "Unit test",
true, true,