mirror of
https://github.com/documize/community.git
synced 2025-08-07 22:45:24 +02:00
Inject different session and audit services for tests
This commit is contained in:
parent
fc57dffc15
commit
06d21f20c8
2 changed files with 255 additions and 7 deletions
69
app/tests/helpers/stub-audit.js
Normal file
69
app/tests/helpers/stub-audit.js
Normal file
|
@ -0,0 +1,69 @@
|
|||
import Ember from 'ember';
|
||||
import netUtil from 'documize/utils/net';
|
||||
import config from 'documize/config/environment';
|
||||
|
||||
const Audit = Ember.Service.extend({
|
||||
sessionService: Ember.inject.service('session'),
|
||||
ready: false,
|
||||
enabled: true,
|
||||
|
||||
init() {
|
||||
this.start();
|
||||
},
|
||||
|
||||
record(id) {
|
||||
if (!this.get('enabled')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.get('ready')) {
|
||||
this.start();
|
||||
}
|
||||
|
||||
// Intercom('trackEvent', id); //jshint ignore: line
|
||||
// Intercom('update'); //jshint ignore: line
|
||||
},
|
||||
|
||||
stop() {
|
||||
// Intercom('shutdown'); //jshint ignore: line
|
||||
},
|
||||
|
||||
start() {
|
||||
let session = this.get('sessionService');
|
||||
|
||||
if (!this.get('enabled') || !session.authenticated || this.get('ready')) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.set('ready', true);
|
||||
|
||||
let appId = config.environment === 'production' ? 'c6cocn4z' : 'itgvb1vo';
|
||||
|
||||
// window.intercomSettings = {
|
||||
// app_id: appId,
|
||||
// name: session.user.firstname + " " + session.user.lastname,
|
||||
// email: session.user.email,
|
||||
// user_id: session.user.id,
|
||||
// "administrator": session.user.admin,
|
||||
// company:
|
||||
// {
|
||||
// id: session.get('appMeta.orgId'),
|
||||
// name: session.get('appMeta.title').string,
|
||||
// "domain": netUtil.getSubdomain(),
|
||||
// "version": session.get('appMeta.version')
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// if (!session.get('isMobile')) {
|
||||
// window.intercomSettings.widget = {
|
||||
// activator: "#IntercomDefaultWidget"
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// window.Intercom('boot', window.intercomSettings);
|
||||
},
|
||||
});
|
||||
|
||||
export default Ember.Test.registerAsyncHelper('stubAudit', function(app, test, attrs={}) {
|
||||
test.register('service:audit', Audit.extend(attrs));
|
||||
});
|
|
@ -1,20 +1,199 @@
|
|||
import Ember from 'ember';
|
||||
import Models from 'documize/utils/model';
|
||||
import models from 'documize/utils/model';
|
||||
import encodingUtil from 'documize/utils/encoding';
|
||||
import netUtil from 'documize/utils/net';
|
||||
|
||||
const Session = Ember.Service.extend({
|
||||
appMeta: Ember.computed(function(){
|
||||
return Models.AppMeta.create();
|
||||
}),
|
||||
|
||||
ready: false,
|
||||
appMeta: null,
|
||||
isMac: false,
|
||||
isMobile: false,
|
||||
previousTransition: null,
|
||||
user: null,
|
||||
authenticated: false,
|
||||
folderPermissions: null,
|
||||
currentFolder: null,
|
||||
|
||||
init: function() {
|
||||
this.set('user', models.UserModel.create());
|
||||
this.appMeta = models.AppMeta.create();
|
||||
|
||||
this.set('isMac', is.mac());
|
||||
this.set('isMobile', is.mobile());
|
||||
},
|
||||
isAdmin: function() {
|
||||
if (this.authenticated && is.not.null(this.user) && this.user.id !== "") {
|
||||
return this.user.admin;
|
||||
}
|
||||
return false;
|
||||
}.property('user'),
|
||||
|
||||
isEditor: function() {
|
||||
if (this.authenticated && is.not.null(this.user) && this.user.id !== "") {
|
||||
return this.user.editor || this.user.admin;
|
||||
}
|
||||
return false;
|
||||
}.property('user'),
|
||||
|
||||
login(credentials) {
|
||||
// TODO: figure out what to do with credentials
|
||||
return new Ember.RSVP.resolve();
|
||||
var self = this;
|
||||
var url = self.appMeta.getUrl('public/authenticate');
|
||||
let domain = netUtil.getSubdomain();
|
||||
|
||||
this.clearSession();
|
||||
|
||||
return new Ember.RSVP.Promise(function(resolve, reject) {
|
||||
if (is.empty(credentials.email) || is.empty(credentials.password)) {
|
||||
reject("invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
var encoded = encodingUtil.Base64.encode(domain + ":" + credentials.email + ":" + credentials.password);
|
||||
var header = {
|
||||
'Authorization': 'Basic ' + encoded
|
||||
};
|
||||
|
||||
$.ajax({
|
||||
url: url,
|
||||
type: 'POST',
|
||||
headers: header,
|
||||
success: function(response) {
|
||||
self.setSession(response.token, models.UserModel.create(response.user));
|
||||
self.get('ready', true);
|
||||
resolve(response);
|
||||
},
|
||||
error: function(reason) {
|
||||
reject(reason);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
boot(){
|
||||
return new Ember.RSVP.resolve();
|
||||
// Goodbye
|
||||
logout: function() {
|
||||
this.clearSession();
|
||||
},
|
||||
|
||||
// Session management
|
||||
setSession: function(token, user) {
|
||||
this.set('user', user);
|
||||
this.set('authenticated', true);
|
||||
|
||||
this.storeSessionItem('token', token);
|
||||
this.storeSessionItem('user', JSON.stringify(user));
|
||||
|
||||
$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
|
||||
jqXHR.setRequestHeader('Authorization', 'Bearer ' + token);
|
||||
});
|
||||
},
|
||||
|
||||
clearSession: function() {
|
||||
this.set('user', null);
|
||||
this.set('authenticated', false);
|
||||
// localStorage.clear();
|
||||
},
|
||||
|
||||
storeSessionItem: function(key, data) {
|
||||
// localStorage[key] = data;
|
||||
// console.log(data);
|
||||
},
|
||||
|
||||
getSessionItem: function(key) {
|
||||
// return localStorage[key];
|
||||
// console.log(data);
|
||||
},
|
||||
|
||||
clearSessionItem: function(key) {
|
||||
// delete localStorage[key];
|
||||
},
|
||||
|
||||
// boot(){
|
||||
// console.log(this.get('appMeta'));
|
||||
// return new Ember.RSVP.resolve();
|
||||
// },
|
||||
|
||||
boot() {
|
||||
let self = this;
|
||||
let dbhash = "";
|
||||
|
||||
if (is.not.null(document.head.querySelector("[property=dbhash]"))) {
|
||||
dbhash = document.head.querySelector("[property=dbhash]").content;
|
||||
}
|
||||
|
||||
if (dbhash.length > 0 && dbhash !== "{{.DBhash}}") {
|
||||
self.get('appMeta').set('orgId', "response.orgId");
|
||||
self.get('appMeta').setSafe('title', "Documize Setup");
|
||||
self.get('appMeta').set('version', "response.version");
|
||||
self.get('appMeta').setSafe('message', "response.message");
|
||||
self.get('appMeta').set('allowAnonymousAccess', false);
|
||||
self.set('ready', true);
|
||||
return new Ember.RSVP.Promise(function(resolve) {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
if (this.get('ready')) {
|
||||
return new Ember.RSVP.Promise(function(resolve) {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
return new Ember.RSVP.Promise(function(resolve) {
|
||||
$.ajax({
|
||||
url: self.get('appMeta').getUrl("public/meta"),
|
||||
type: 'GET',
|
||||
contentType: 'json',
|
||||
success: function(response) {
|
||||
self.get('appMeta').set('orgId', response.orgId);
|
||||
self.get('appMeta').setSafe('title', response.title);
|
||||
self.get('appMeta').set('version', response.version);
|
||||
self.get('appMeta').setSafe('message', response.message);
|
||||
self.get('appMeta').set('allowAnonymousAccess', response.allowAnonymousAccess);
|
||||
|
||||
let token = self.getSessionItem('token');
|
||||
|
||||
if (is.not.undefined(token)) {
|
||||
// We now validate current token
|
||||
let tokenCheckUrl = self.get('appMeta').getUrl(`public/validate?token=${token}`);
|
||||
|
||||
$.ajax({
|
||||
url: tokenCheckUrl,
|
||||
type: 'GET',
|
||||
contentType: 'json',
|
||||
success: function(user) {
|
||||
self.setSession(token, models.UserModel.create(user));
|
||||
self.set('ready', true);
|
||||
resolve();
|
||||
},
|
||||
error: function(reason) {
|
||||
if (reason.status === 401 || reason.status === 403) {
|
||||
localStorage.clear();
|
||||
window.location.href = "/auth/login";
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
self.set('ready', true);
|
||||
resolve();
|
||||
}
|
||||
},
|
||||
error: function(reason) {
|
||||
if (reason.status === 401 || reason.status === 403) {
|
||||
window.location.href = "https://documize.com";
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
getSessionItem(key){
|
||||
return this.get(`data.${key}`);
|
||||
},
|
||||
|
||||
sso: function(credentials) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue