Aurelia - Trying to switch root and it cannot find the location - aurelia

I have been following a variety of sources in particular Mathew James Davis with THIS blog entry and others.
I had in my boot.ts (main in other examples etc):
aurelia
.start()
.then(function () { return aurelia.setRoot(PLATFORM.moduleName("public/public/public")); });
});
which worked.
I am now running two roots and checking if a JWT exists in localstorage. It gets the jwt and stores it OK and I even obtain it correctly but it now doesnt recognise the root and I think I have a syntax error of some sort..
This is what I now have:
aurelia.start().then(() => {
var auth = aurelia.container.get(AuthService);
let root = auth.isAuthenticated() ? 'app/app/app' : 'public/public/public';
return aurelia.setRoot(PLATFORM.moduleName(root));
});
I am getting the following error:
Uncaught (in promise) Error: Unable to find module with ID: public/public/public
I do not know why it now doesnt work when the exact same path worked when entered directly..
UPDATE
Ok so I thought I would refactor this and utilise the section of code that works:
This is what I changed it to:
var auth = aurelia.container.get(AuthService);
let authenticated = auth.isAuthenticated();
console.log("authenticated: ", authenticated);
if (authenticated) {
aurelia
.start()
.then(function () { return aurelia.setRoot(PLATFORM.moduleName("app/app/app")); });
} else {
aurelia
.start()
.then(function () { return aurelia.setRoot(PLATFORM.moduleName("public/public/public")); });
}
Now it shows a blank loading page with "loading" and shows this at in the console:
authenticated: false
aurelia-logging-console.js:27 INFO [aurelia] Aurelia Started
client.js:82 [HMR] connected
Note - if just have the original code with app/app/app in it and authentication= true then it also shows correctly.
So both roots work but not with a check..

Finally fixed this. I eventually changed my original code:
aurelia.start().then(function () {
var auth = aurelia.container.get(AuthService);
var root = auth.isAuthenticated() ? "app/app/app" : "public/public/public";
aurelia.setRoot(root);
to:
aurelia.start().then(() => {
var auth = aurelia.container.get(AuthService);
let root: string = auth.isAuthenticated() ? PLATFORM.moduleName('app/app/app') : PLATFORM.moduleName('public/public/public');
aurelia.setRoot(root, document.body)
});
and it now works. In the end it was my lack of programming skills however if anybody else has a problem they might try this for switching routes.

Related

casl - can() returns null after reloading (F5) page

I'm trying to setting up CASL permissions system into my VueJS 2 project.
All works fine, until I intentionally refresh (Like F5). All can() returns false even if my user have the abilities.
router / index.js :
router.beforeEach((to, _, next) => {
const isLoggedIn = isUserLoggedIn()
if (!canNavigate(to)) {
// Redirect to login if not logged in
if (!isLoggedIn) return next({ name: 'auth-login' })
// If logged in => not authorized
return next({ name: 'misc-not-authorized' })
}
// Redirect if logged in
if (to.meta.redirectIfLoggedIn && isLoggedIn) {
const userData = getUserData()
next(getDashboardRoute(userData ? userData.role : null))
}
return next()
})
canNavigate(to) function :
export const canNavigate = to => ability.can(to.meta.action || 'read', to.meta.resource)
user abilities (from localStorage) :
route configuration :
export default [
{
path: '/route-test/',
name: 'route-test',
component: () => import('#/views/TestRoute.vue'),
meta: {
resource: 'ADB',
action: 'read',
},
},
]
So, canNavigate returns false, and I'm getting a Maximum call stack size exceeded error but, this is normal due to the "infinite" loop with in my beforeEach router function...
Why do my canNavigate returns false... after refresh?
Thanks to everyone give time to help me :)
I'm facing the same issue with a Vue 2 project - everytime i refresh the page the $ability instance gets reseted to its default value (before the .update() is called). Idk what's the root cause of the problem, but what i made solves the problem for now: I update the $ability instance each time the entry point component (in my case Home component) is mounted(). Idk if it is a good solution, but worked for me (if anyone knows a better way to do it please let me know).

TestCafe: Changing download location

I am trying to change the download location and I found these codes doing research (sorry I forgot where I got these)
const browserConnection = t.testRun.browserConnection;
const client = browserConnection.provider.plugin.openedBrowsers[browserConnection.id].client;
const { Network, Page } = client;
const downloadDirectory = '../my_downloads');
await Promise.all([
Network.enable(),
Page.enable()
]);
Network.requestWillBeSent((param) => {
// console.log("Network.requestWillBeSent: " + JSON.stringify(param));
});
Network.responseReceived((param) => {
// console.log("Network.responseReceived: " + JSON.stringify(param));
});
await Page.setDownloadBehavior({
behavior: 'allow',
downloadPath: downloadDirectory
});
It was working perfectly fine using version 10.9.2 and this version was installed globally. I updated my TestCafe to 1.10.1 locally installed and now got this error:
TypeError: Cannot destructure property 'Network' of 'client' as it is undefined.
Any inputs are well appreciated. And looking forward to it :)
This internal API has changed due to testing support of multiple windows. Please use the getActiveClient method:
const browserConnection = t.testRun.browserConnection;
const runtimeInfo = rowserConnection.provider.plugin.openedBrowsers[browserConnection.id];
const { Network, Page } = await runtimeInfo.browserClient.getActiveClient();
If you need to change the download location to read and check a file from it, please use a public API for this: example.

Confirm URL using POM Nightwatch

New to nightwatch and js in general and I'm struggling to figure out how to validate a url using pom pattern. I know this is wrong. Any suggestions are greatly appreciated, including useful links for me to rtfm as I'm struggling to find robust examples of pom nightwatch.
test.js
module.exports = {
"tags": ['sanity'],
'confirm navigation to example.com' : function (client) {
var landingPage = client.page.landing();
landingPage.navigate();
landingPage.confirmOnLandingPage();
client.end();
}
};
page.js
var landingCommands = {
navigate:function(){
url: 'example.com/'
},
confirmOnLandingPage:function(){
this.expect.element('body')
.to.be.present.before(1000)
.url(function(result)
{
this.assert.equal(result.value, 'example.com/', 'On Landing Page.')
});
}
}
Running: confirm navigation to example.com
✖ TypeError:
this.expect.element(...).to.be.present.before(...).url is not a
function
at Page.confirmOnLandingPage (/Users/Home/Development/NWQA/pages/page.js:9:10)
at Object.confirm navigation to example.com (/Users/Home/Development/NWQA/tests/test.js:7:21)
FAILED: 1 errors (18ms)
After running .expect you break the Nightwatch command chain and start Expect.js chain so after you call this.expect.element('body').to.be.present.before(1000) you get Expect.js object and not Nightwatch browser object.
To fix just start a new chain and change this.url call to this.api.url since url() is not available within the page object:
confirmOnLandingPage: function(){
this.expect.element('body').to.be.present.before(1000);
this.api.url(function(result)
{
this.assert.equal(result.value, 'example.com/', 'On Landing Page.')
});
}
Update:
I just noticed you incorrectly declared URL for your page object. navigate is internal function, you only need to provide url property:
var landingCommands = {
url: 'example.com/',
...
};

mocha 'after' fails saying it can't find 'app'

Ok, my mocha tests will pass if I comment out the 'before' and 'after' methods. I am sure that both of my errors are related to each other.
The 'after' method fails stating app.close isn't a function. The 'before' method fails saying it cant find 'app' on my line 7 (clearing server cache).
I am completely out of options or ideas. I would like to be able to start and stop my server at my command. This is the first time that I have attempted to include any type of 'before/after' methods to my mocha testing. working code below, but with my failing portion commented out. Any suggestions??
var request = require('supertest');
var app = require('../../server');
describe('server', function() {
before(function () {
//var app = require('../../server')();
//delete require.cache[require.resolve('app')];
});
after(function () {
//app.close();
});
describe('basic comms', function() {
it('responds to root route', function testSlash(done) {
request(app)
.get('/')
.expect('Content-type', /json/)
//.expect(res.message).to.equal('Hello World!')
.expect(200, done);
});
it('404 everything else', function testPath(done) {
//console.log('testing 404 response');
request(app)
.get('/foo/bar')
.expect(404, done);
});
});
});
In before you require your app in a different way than in line 2. Why would you not use already required app?
Example:
before(function () {
// here you can use app from line 2
});
Regarding app.close, where did you find this function?
Check Express docs:
http://expressjs.com/en/4x/api.html#app
To close express server, you can use this approach:
how to properly close node-express server?

In cloud code seems impossible to use Parse.Config.get() with express is it correct?

Is there any way to use Parse.Config.get() inside an expressjs app hosted in cloud code?
Looks very easy to use Parse.Object and Parse.User but with Parse.Config.get() the code is not deployed using "parse deploy"
We manage to use it adding the jssdk in html and using "frontend js" but haven't find any way to use in directly in express controllers.
Thanks
It seem to be related with some kind of permissions issues...
var Parse = require('parse-cloud-express').Parse;
var Util = require('util')
Parse.Cloud.define("currentConfig", function(request, response) {
console.log('Ran currentConfig cloud function.');
// why do I have to do this?
Parse.initialize(xxx, yyy);
Parse.Config.get().then(function(config) {
// never called
// ...
console.log(config.get('xxx'))
}, function(error) {
console.log(Util.inspect(error))
});
});
Output
Ran currentConfig cloud function.
{ code: undefined, message: 'unauthorized' }
Edited code which work for me:
var Parse = require('parse-cloud-express').Parse;
var Util = require('util')
Parse.initialize("appId", "restApiKey", "masterKey");
Parse.Cloud.define("currentConfig", function(request, response) {
console.log('Ran currentConfig cloud function.');
Parse.Cloud.useMasterKey();
Parse.Config.get().then(function(config) {
// never called
// ...
console.log(config.get('xxx'))
}, function(error) {
console.log(Util.inspect(error))
});
});
EDIT: Add solution :)