mirror of
https://github.com/ansible/awx.git
synced 2026-05-11 03:17:38 -02:30
committing first pass at unit test for SocketService
This commit is contained in:
@@ -74,7 +74,6 @@ import './shared/Modal';
|
|||||||
import './shared/prompt-dialog';
|
import './shared/prompt-dialog';
|
||||||
import './shared/directives';
|
import './shared/directives';
|
||||||
import './shared/filters';
|
import './shared/filters';
|
||||||
import socket from './shared/socket/main';
|
|
||||||
import './shared/features/main';
|
import './shared/features/main';
|
||||||
import config from './shared/config/main';
|
import config from './shared/config/main';
|
||||||
import './login/authenticationServices/pendo/ng-pendo';
|
import './login/authenticationServices/pendo/ng-pendo';
|
||||||
@@ -195,7 +194,6 @@ var tower = angular.module('Tower', [
|
|||||||
'StandardOutHelper',
|
'StandardOutHelper',
|
||||||
'LogViewerOptionsDefinition',
|
'LogViewerOptionsDefinition',
|
||||||
'JobDetailHelper',
|
'JobDetailHelper',
|
||||||
socket.name,
|
|
||||||
'lrInfiniteScroll',
|
'lrInfiniteScroll',
|
||||||
'LoadConfigHelper',
|
'LoadConfigHelper',
|
||||||
'PortalJobsListDefinition',
|
'PortalJobsListDefinition',
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import stringFilters from './string-filters/main';
|
|||||||
import truncatedText from './truncated-text.directive';
|
import truncatedText from './truncated-text.directive';
|
||||||
import stateExtender from './stateExtender.provider';
|
import stateExtender from './stateExtender.provider';
|
||||||
import rbacUiControl from './rbacUiControl';
|
import rbacUiControl from './rbacUiControl';
|
||||||
|
import socket from './socket/main';
|
||||||
|
|
||||||
export default
|
export default
|
||||||
angular.module('shared', [listGenerator.name,
|
angular.module('shared', [listGenerator.name,
|
||||||
@@ -18,6 +19,7 @@ angular.module('shared', [listGenerator.name,
|
|||||||
stringFilters.name,
|
stringFilters.name,
|
||||||
'ui.router',
|
'ui.router',
|
||||||
rbacUiControl.name
|
rbacUiControl.name
|
||||||
|
socket.name
|
||||||
])
|
])
|
||||||
.factory('lodashAsPromised', lodashAsPromised)
|
.factory('lodashAsPromised', lodashAsPromised)
|
||||||
.directive('truncatedText', truncatedText)
|
.directive('truncatedText', truncatedText)
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
*************************************************/
|
*************************************************/
|
||||||
import ReconnectingWebSocket from 'reconnectingwebsocket';
|
import ReconnectingWebSocket from 'reconnectingwebsocket';
|
||||||
export default
|
export default
|
||||||
['$rootScope', '$location', '$log', 'Authorization','$state',
|
['$rootScope', '$location', '$log','$state',
|
||||||
function ($rootScope, $location, $log, Authorization, $state) {
|
function ($rootScope, $location, $log, $state) {
|
||||||
return {
|
return {
|
||||||
init: function() {
|
init: function() {
|
||||||
var self = this,
|
var self = this,
|
||||||
@@ -35,36 +35,9 @@ export default
|
|||||||
self.checkStatus();
|
self.checkStatus();
|
||||||
};
|
};
|
||||||
|
|
||||||
self.socket.onmessage = function (e) {
|
self.socket.onmessage = this.onMessage;
|
||||||
$log.debug('Received From Server: ' + e.data);
|
|
||||||
var data = JSON.parse(e.data), str = "";
|
|
||||||
if(data.group_name==="jobs" && !('status' in data)){
|
|
||||||
// we know that this must have been a
|
|
||||||
// summary complete message
|
|
||||||
$log.debug('Job summary_complete ' + data.unified_job_id);
|
|
||||||
$rootScope.$emit('ws-jobs-summary', data);
|
|
||||||
}
|
|
||||||
else if(data.group_name==="job_events"){
|
|
||||||
str = `ws-${data.group_name}-${data.job}`;
|
|
||||||
}
|
|
||||||
else if(data.group_name==="ad_hoc_command_events"){
|
|
||||||
str = `ws-${data.group_name}-${data.ad_hoc_command}`;
|
|
||||||
}
|
|
||||||
else if(data.group_name==="control"){
|
|
||||||
$log.debug(data.reason);
|
|
||||||
$rootScope.sessionTimer.expireSession('session_limit');
|
|
||||||
$state.go('signOut');
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// The naming scheme is "ws" then a
|
|
||||||
// dash (-) and the group_name.
|
|
||||||
// ex: 'ws-jobs'
|
|
||||||
str = `ws-${data.group_name}`;
|
|
||||||
}
|
|
||||||
$rootScope.$emit(str, data);
|
|
||||||
return self.socket;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
return self.socket;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// encountered expired token, redirect to login page
|
// encountered expired token, redirect to login page
|
||||||
@@ -77,6 +50,35 @@ export default
|
|||||||
$log.debug('socket status: ' + $rootScope.socketStatus);
|
$log.debug('socket status: ' + $rootScope.socketStatus);
|
||||||
}, 2000);
|
}, 2000);
|
||||||
},
|
},
|
||||||
|
onMessage: function(e){
|
||||||
|
$log.debug('Received From Server: ' + e.data);
|
||||||
|
var data = JSON.parse(e.data), str = "";
|
||||||
|
if(data.group_name==="jobs" && !('status' in data)){
|
||||||
|
// we know that this must have been a
|
||||||
|
// summary complete message b/c status is missing
|
||||||
|
$log.debug('Job summary_complete ' + data.unified_job_id);
|
||||||
|
$rootScope.$emit('ws-jobs-summary', data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if(data.group_name==="job_events"){
|
||||||
|
str = `ws-${data.group_name}-${data.job}`;
|
||||||
|
}
|
||||||
|
else if(data.group_name==="ad_hoc_command_events"){
|
||||||
|
str = `ws-${data.group_name}-${data.ad_hoc_command}`;
|
||||||
|
}
|
||||||
|
else if(data.group_name==="control"){
|
||||||
|
$log.debug(data.reason);
|
||||||
|
$rootScope.sessionTimer.expireSession('session_limit');
|
||||||
|
$state.go('signOut');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// The naming scheme is "ws" then a
|
||||||
|
// dash (-) and the group_name.
|
||||||
|
// ex: 'ws-jobs'
|
||||||
|
str = `ws-${data.group_name}`;
|
||||||
|
}
|
||||||
|
$rootScope.$emit(str, data);
|
||||||
|
},
|
||||||
disconnect: function(){
|
disconnect: function(){
|
||||||
if(this.socket){
|
if(this.socket){
|
||||||
this.socket.close();
|
this.socket.close();
|
||||||
|
|||||||
31
awx/ui/tests/spec/socket/socket.service-test.js
Normal file
31
awx/ui/tests/spec/socket/socket.service-test.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
describe('Service: SocketService', () => {
|
||||||
|
|
||||||
|
let SocketService,
|
||||||
|
rootScope,
|
||||||
|
event;
|
||||||
|
|
||||||
|
beforeEach(angular.mock.module('shared'));
|
||||||
|
beforeEach(angular.mock.module('socket', function($provide){
|
||||||
|
$provide.value('$rootScope', rootScope);
|
||||||
|
$provide.value('$location', {url: function(){}});
|
||||||
|
}));
|
||||||
|
beforeEach(angular.mock.inject(($rootScope, _SocketService_) => {
|
||||||
|
rootScope = $rootScope.$new();
|
||||||
|
rootScope.$emit = jasmine.createSpy('$emit');
|
||||||
|
SocketService = _SocketService_;
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('socket onmessage() should broadcast to correct event listener', function(){
|
||||||
|
|
||||||
|
it('should send to ws-jobs-summary', function(){
|
||||||
|
event = {data : {group_name: "jobs"}};
|
||||||
|
event.data = JSON.stringify(event.data);
|
||||||
|
console.log(event);
|
||||||
|
SocketService.onMessage(event);
|
||||||
|
expect(rootScope.$emit).toHaveBeenCalledWith('ws-jobs-summary', event.data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user