Skip to content

Commit

Permalink
Merge pull request #43 from xmpp-grid-broker/XGB-190-don-t-show-disco…
Browse files Browse the repository at this point in the history
…nnected-dialog

[#XGB-190] Don't show disconnected dialog
  • Loading branch information
raphiz committed May 25, 2018
2 parents 7121680 + 3f350aa commit 87e402e
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 189 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"ng": "0.0.0-rc6",
"rxjs": "^5.5.6",
"spectre.css": "^0.5.1",
"stanza.io": "^v9.0.2",
"stanza.io": "^v9.0.6",
"jxt-xmpp": "3.2.2",
"stylelint": "^9.2.0",
"xmpp-jid": "^1.2.3",
Expand Down
50 changes: 35 additions & 15 deletions src/app/core/config.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import {Observable} from 'rxjs/Observable';
import {ConfigService} from './config.service';
import {Config, XmppConfig, XmppTransport} from './models/config';
import {XmppService} from './xmpp/xmpp.service';


class FakeErrorResponse {
constructor(readonly message: string, status: number, statusText: string) {}
constructor(readonly message: string, status: number, statusText: string) {
}
}

class FakeHttpClient {
constructor(public fake_url: string, public fake_result: any = {}) {}
constructor(public fake_url: string, public fake_result: any = {}) {
}

get(url: string): Observable<any> {
if (url === this.fake_url) {
Expand All @@ -24,6 +27,11 @@ class FakeHttpClient {
describe('ConfigService', () => {
let httpClient, service: ConfigService;

let xmppService: jasmine.SpyObj<XmppService>;
beforeEach(() => {
xmppService = jasmine.createSpyObj('XmppService', ['initialize']);
});

const JSON_CONFIG = {
xmpp: {
server: 'openfire',
Expand All @@ -44,37 +52,49 @@ describe('ConfigService', () => {
it('should request the configuration file', done => {
httpClient = new FakeHttpClient('./configuration.json');
spyOn(httpClient, 'get').and.callFake(() => Observable.of(JSON_CONFIG));
service = new ConfigService(httpClient);
service = new ConfigService(httpClient, xmppService);

service.getConfig().then(config => {
expect(httpClient.get).toHaveBeenCalledWith(httpClient.fake_url);
done();
});
}).catch(err => fail(err));
});

it('should call initialize on the xmpp service', done => {
httpClient = new FakeHttpClient('./configuration.json');
spyOn(httpClient, 'get').and.callFake(() => Observable.of(JSON_CONFIG));
service = new ConfigService(httpClient, xmppService);

service.getConfig().then(config => {
expect(xmppService.initialize).toHaveBeenCalledWith(config);
done();
}).catch(err => fail(err));
});


it('should parse the configuration json correctly', done => {
httpClient = new FakeHttpClient('./configuration.json');
spyOn(httpClient, 'get').and.callFake(() => Observable.of(JSON_CONFIG));
service = new ConfigService(httpClient);
service = new ConfigService(httpClient, xmppService);

service.getConfig().then((config: Config) => {
const required = ['transport', 'boshUrl', 'useStreamManagement'];
const required = ['transport', 'boshUrl', 'useStreamManagement'];
for (const name of required) {
expect(config.xmpp[name]).toBe(REFERENCE_CONFIG.xmpp[name]);
}

expect(config.xmpp.server).toBe(REFERENCE_CONFIG.xmpp.server);
expect(config.xmpp.sasl[0]).toBe(REFERENCE_CONFIG.xmpp.sasl[0]);
done();
});
}).catch(err => fail(err));
});

it('should throw error if configuration file does not exist', (done) => {
it('should throw error if configuration file does not exist', (done) => {
httpClient = new FakeHttpClient('./bla.json');

// Catch asynchronously thrown error from ConfigService.constructor
const initialisation = async() => {
service = new ConfigService(httpClient);
const initialisation = async () => {
service = new ConfigService(httpClient, xmppService);
await service.getConfig();
};

Expand All @@ -86,9 +106,9 @@ describe('ConfigService', () => {
spyOn(httpClient, 'get').and.callFake(() => Observable.of({}));

// Catch asynchronously thrown error from ConfigService.constructor
const initialisation = async() => {
service = new ConfigService(httpClient);
return await service.getConfig();
const initialisation = async () => {
service = new ConfigService(httpClient, xmppService);
return await service.getConfig();
};

initialisation().then(() => fail('No error thrown!'), () => done());
Expand All @@ -102,8 +122,8 @@ describe('ConfigService', () => {
spyOn(httpClient, 'get').and.callFake(() => Observable.of(incomplete_json_config));

// Catch asynchronously thrown error from ConfigService.constructor
const initialisation = async() => {
service = new ConfigService(httpClient);
const initialisation = async () => {
service = new ConfigService(httpClient, xmppService);
return await service.getConfig();
};

Expand Down
23 changes: 15 additions & 8 deletions src/app/core/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Config} from './models/config';
import {environment} from '../../environments/environment';
import {XmppService} from './xmpp/xmpp.service';

@Injectable()
export class ConfigService {
Expand All @@ -12,22 +13,28 @@ export class ConfigService {
static readonly CONFIG_FILE = environment.config_url;
private readonly _config: Promise<Config>;

constructor(private http: HttpClient) {
constructor(private http: HttpClient,
private xmppService: XmppService) {
this._config = this.loadConfig();
}

loadConfig(): Promise<Config> {
return this.http.get(ConfigService.CONFIG_FILE)
.toPromise()
.then(json => Config.fromJson(json));
}

/**
* Returns a Promise of the application configuration,
* that is loaded on this services initialisation.
* @returns {Promise<Config>}
*/
getConfig(): Promise<Config> {
public getConfig(): Promise<Config> {
return this._config;
}

private loadConfig(): Promise<Config> {
return this.http.get(ConfigService.CONFIG_FILE)
.toPromise()
.then(json => {
const config = Config.fromJson(json);
this.xmppService.initialize(config);
return config;
});
}

}
22 changes: 9 additions & 13 deletions src/app/core/xmpp/xmpp-feature-detection.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ class FakeXmppService {
executeIqToPubsub(cmd: any): Promise<any> {
return this.executeIq(cmd);
}
}

class FakeConfigService {
getConfig(): Promise<any> {
return Promise.resolve({xmpp: {jid: {domain: 'openfire'}}});
getServerTitle(): string {
return 'openfire';
}
}

Expand All @@ -29,16 +26,15 @@ describe('XmppFeatureService', () => {
PUBSUB_IQ_DISCOVERY_RESULT.discoInfo.features.push('http://jabber.org/protocol/pubsub');

const xmppService: any = new FakeXmppService();
const configService: any = new FakeConfigService();

beforeEach(() => {
xmppSpy = spyOn(xmppService, 'executeIq');
logSpy = jasmine.createSpyObj('ErrorLog', ['warn']);
service = new XmppFeatureService(xmppService, configService, logSpy);
service = new XmppFeatureService(xmppService, logSpy);
});

it('should detect a supported feature', done => {
xmppSpy.and.returnValue(PUBSUB_IQ_DISCOVERY_RESULT);
xmppSpy.and.returnValue(Promise.resolve(PUBSUB_IQ_DISCOVERY_RESULT));
service.checkFeature('pubsub', 'subscribe').then(result => {
expect(xmppSpy).toHaveBeenCalled();
expect(result).toBe(true);
Expand All @@ -47,7 +43,7 @@ describe('XmppFeatureService', () => {
});

it('should detect a supported protocol', done => {
xmppSpy.and.returnValue(PUBSUB_IQ_DISCOVERY_RESULT);
xmppSpy.and.returnValue(Promise.resolve(PUBSUB_IQ_DISCOVERY_RESULT));
service.checkFeature('pubsub').then(result => {
expect(xmppSpy).toHaveBeenCalled();
expect(result).toBe(true);
Expand All @@ -56,7 +52,7 @@ describe('XmppFeatureService', () => {
});

it('should detect an unsupported feature', done => {
xmppSpy.and.returnValue(PUBSUB_IQ_DISCOVERY_RESULT);
xmppSpy.and.returnValue(Promise.resolve(PUBSUB_IQ_DISCOVERY_RESULT));
const protocol = 'pubsub';
const feature = 'do-magic';
service.checkFeature(protocol, feature).then(result => {
Expand All @@ -68,7 +64,7 @@ describe('XmppFeatureService', () => {
});

it('should detect if one of multiple features is unsupported', done => {
xmppSpy.and.returnValue(PUBSUB_IQ_DISCOVERY_RESULT);
xmppSpy.and.returnValue(Promise.resolve(PUBSUB_IQ_DISCOVERY_RESULT));
const protocol = 'pubsub';
const feature = 'do-magic';
service.checkFeatures(protocol, [feature, 'subscribe']).then(result => {
Expand All @@ -81,7 +77,7 @@ describe('XmppFeatureService', () => {
});

it('should detect if all of multiple features are supported', done => {
xmppSpy.and.returnValue(PUBSUB_IQ_DISCOVERY_RESULT);
xmppSpy.and.returnValue(Promise.resolve(PUBSUB_IQ_DISCOVERY_RESULT));
service.checkFeatures('pubsub', ['collections', 'subscribe']).then(result => {
expect(xmppSpy).toHaveBeenCalled();
expect(result.length).toBe(0);
Expand All @@ -94,7 +90,7 @@ describe('XmppFeatureService', () => {
discovery_result.discoInfo.features = discovery_result.discoInfo.features
.filter(feature => !feature.endsWith('collections'));

xmppSpy.and.returnValue(discovery_result);
xmppSpy.and.returnValue(Promise.resolve(discovery_result));
service.getMissingRequiredFeatures().then(result => {
expect(xmppSpy).toHaveBeenCalled();
expect(result.length).toBe(1);
Expand Down
43 changes: 29 additions & 14 deletions src/app/core/xmpp/xmpp-feature-guard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import {ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot} from '@angular
import {XmppFeatureService} from './xmpp-feature.service';
import {NotificationService} from '../notifications/notification.service';
import {Injectable} from '@angular/core';
import {ConfigService} from '../config.service';

@Injectable()
export class XmppFeatureGuardService implements CanActivate {
private checkWasSuccessfulOnce: boolean;


constructor(private xmppFeatureService: XmppFeatureService,
private configService: ConfigService,
private notificationService: NotificationService) {
this.checkWasSuccessfulOnce = false;
}
Expand All @@ -17,21 +19,34 @@ export class XmppFeatureGuardService implements CanActivate {
if (this.checkWasSuccessfulOnce) {
return true;
}
return this.xmppFeatureService.getMissingRequiredFeatures()
.then(missingFeatures => {
const successful = missingFeatures.length === 0;
if (!successful) {
this.notificationService.alert(
'Configuration Problem',
'Not all required XMPP features are supported. ' +
'The missing features are listed in the box below:',
false,
missingFeatures);
} else {
this.checkWasSuccessfulOnce = true;
}
return successful;
return this.configService.getConfig()
.then(() => this.xmppFeatureService.getMissingRequiredFeatures()
.then(missingFeatures => {
const successful = missingFeatures.length === 0;
if (!successful) {
this.notificationService.alert(
'Configuration Problem',
'Not all required XMPP features are supported. ' +
'The missing features are listed in the box below:',
false,
missingFeatures);
} else {
this.checkWasSuccessfulOnce = true;
}
return successful;
})
).catch((e) => {
this.notificationService.alert(
'Failed to load the configuration',
'Learn how to how to correctly set up this application in the ' +
'<a href="https://github.com/xmpp-grid-broker/xmpp-grid-broker/blob/master/docs/INSTALL.adoc">' +
'Installation Guide</a>.',
false,
undefined,
true);
return false;
});

}

}
Loading

0 comments on commit 87e402e

Please sign in to comment.