Skip to content

Commit

Permalink
Tests done for solid backend
Browse files Browse the repository at this point in the history
  • Loading branch information
yasharpm committed Aug 11, 2024
1 parent 4a55b71 commit 07bcf1c
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 36 deletions.
2 changes: 1 addition & 1 deletion release/remotestorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -50299,7 +50299,7 @@ class Authorize {
* browser will navigate back to redirectUri and OAuth will continue
* with onFeaturesLoaded.
*/
static authorize(remoteStoFrage, options) {
static authorize(remoteStorage, options) {
(0, log_1.default)('[Authorize] authURL = ', options.authURL, 'scope = ', options.scope, 'redirectUri = ', options.redirectUri, 'clientId = ', options.clientId, 'response_type =', options.response_type);
if (!options.scope) {
throw new Error("Cannot authorize due to undefined or empty scope; did you forget to access.claim()?");
Expand Down
2 changes: 1 addition & 1 deletion release/remotestorage.js.map

Large diffs are not rendered by default.

File renamed without changes.
59 changes: 36 additions & 23 deletions src/solid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
localStorageAvailable
} from './util';
import {Remote, RemoteBase, RemoteResponse, RemoteSettings} from "./remote";
import ConfigObserver from "./solid/configObserver";
import ConfigStorage from "./solid/solidStorage";
import ConfigObserver from "./interfaces/configObserver";
import ConfigStorage from "./solidStorage";
import Blob from "blob";

const SETTINGS_KEY = 'remotestorage:solid';
Expand Down Expand Up @@ -174,13 +174,12 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {
*
* @protected
*/
configure (settings: RemoteSettings) { // Settings parameter compatible with WireClient
// TODO fix comments
configure (settings: RemoteSettings) {
// We only update this.userAddress if settings.userAddress is set to a string or to null
if (typeof settings.userAddress !== 'undefined') { this.userAddress = settings.userAddress; }
// We only update this.userAddress if settings.userAddress is set to a string or to null
// We only update this.authURL if settings.href is set to a string or to null
if (typeof settings.href !== 'undefined') { this.authURL = settings.href; }
// Same for this.token. If only one of these two is set, we leave the other one at its existing value
// Read session properties and pod URL from the properties if it exists
if (typeof settings.properties !== 'undefined') {
const properties = settings.properties as {sessionProperties: object, podURL: string};

Expand Down Expand Up @@ -227,9 +226,7 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {

if (this.sessionProperties) {
this.configStorage.setConfig(JSON.stringify(this.sessionProperties));
this.connected = false;

// TODO this.connect();
this.connected = this.session.info && this.session.info.isLoggedIn;
writeSettingsToCache.apply(this);
} else {
handleError.apply(this);
Expand All @@ -253,25 +250,36 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {
}

setPodURL(podURL: string): void {
if (this.selectedPodURL === podURL) {
return;
}

this.selectedPodURL = podURL;

if (this.session.info && this.session.info.isLoggedIn) {
let settings = getJSONFromLocalStorage(SETTINGS_KEY);
if (!settings) {
settings = { };
if (this.selectedPodURL) {
let settings = getJSONFromLocalStorage(SETTINGS_KEY);
if (!settings) {
settings = { };
}

settings.userAddress = this.session.info.webId;
settings.href = this.authURL;
settings.properties = {
sessionProperties: this.sessionProperties,
podURL: this.selectedPodURL
};

localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings));

this.connected = true;
this._emit('connected');
}
else {
this.connected = false;
this.rs._emit('pod-not-selected');
}

settings.userAddress = this.session.info.webId;
settings.href = this.authURL;
settings.properties = {
sessionProperties: this.sessionProperties,
podURL: this.selectedPodURL
};

localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings));
}

this._emit('connected');
}

getPodURL(): string|null {
Expand All @@ -284,6 +292,11 @@ class Solid extends RemoteBase implements Remote, ConfigObserver {
connect (): void {
this.rs.setBackend('solid');

if (!this.authURL) {
this.rs._emit('error', new Error(`No authURL is configured.`));
return;
}

this.session.login({
oidcIssuer: this.authURL,
redirectUrl: new URL("/", window.location.href).toString(),
Expand Down
2 changes: 1 addition & 1 deletion src/solid/solidStorage.ts → src/solidStorage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
IStorage,

Check failure on line 2 in src/solidStorage.ts

View workflow job for this annotation

GitHub Actions / node.js (18)

Expected indentation of 2 spaces but found 4

Check failure on line 2 in src/solidStorage.ts

View workflow job for this annotation

GitHub Actions / node.js (20)

Expected indentation of 2 spaces but found 4
} from "@inrupt/solid-client-authn-browser";
import ConfigObserver from "./configObserver";
import ConfigObserver from "./interfaces/configObserver";

class BrowserStorage implements IStorage {
get storage(): typeof window.localStorage {
Expand Down
65 changes: 55 additions & 10 deletions test/unit/solid-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,31 +95,76 @@ define(['util', 'require', './build/eventhandling', './build/solid',

var tests = [
{
desc: "#configure sets 'connected' to true, once sessionProperties is given",
desc: "#configure sets userAddress when given",
run: function (env, test) {
env.client.configure({
sessionProperties: { }
userAddress: '[email protected]'
});
test.assert(env.client.connected, true);
test.assert(env.client.userAddress, '[email protected]');
}
},

{
desc: "#configure sets userAddress when given",
desc: "#configure sets authURL when given",
run: function (env, test) {
env.client.configure({
userAddress: '[email protected]'
href: 'https://solidcommunity.net'
});
test.assert(env.client.userAddress, '[email protected]');
test.assert(env.client.authURL, 'https://solidcommunity.net');
}
},


{
desc: "#configure sets sessionProperties when given",
run: function (env, test) {
env.client.configure({
properties: {
sessionProperties: { check: true }
}
});
test.assert(env.client.sessionProperties, { check: true });
}
},
{
desc: "#configure sets podURL when given",
run: function (env, test) {
env.client.configure({
properties: {
podURL: 'https://example.solidcommunity.net/'
}
});
test.assert(env.client.selectedPodURL, 'https://example.solidcommunity.net/');
}
},
{
desc: "#setAuthURL will update auth URL",
run: function (env, test) {
env.client.setAuthURL('https://solidcommunity.net');
test.assert(env.client.authURL, 'https://solidcommunity.net');
}
},
{
desc: "#setPodURL will update the selected pod URL",
run: function (env, test) {
env.client.setPodURL('https://example.solidcommunity.net/');
test.assert(env.client.selectedPodURL, 'https://example.solidcommunity.net/');
}
},
{
desc: "#connect will emit error if the auth URL is not set",
run: function (env, test) {
const errorCheck = { hasError: false };
env.rs.on('error', function(error) {
test.assert(error.message, 'No authURL is configured.');
errorCheck.hasError = true;
});
env.client.connect();
test.assert(errorCheck.hasError, true);
}
}
];

suites.push({
name: "Solid",
desc: "Solid back-end",
desc: "session configuration & setup",
setup: setup,
beforeEach: beforeEach,
afterEach: afterEach,
Expand Down

0 comments on commit 07bcf1c

Please sign in to comment.