Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmackay committed Nov 9, 2016
2 parents 569c764 + bfd60e1 commit ec638b2
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 33 deletions.
12 changes: 8 additions & 4 deletions app/common/auth/authentication-events.run.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = AuthenticationEvents;

AuthenticationEvents.$inject = ['$rootScope', '$location', 'Authentication', 'Session', '_'];
function AuthenticationEvents($rootScope, $location, Authentication, Session, _) {
AuthenticationEvents.$inject = ['$rootScope', '$location', 'Authentication', 'Session', '_', '$route'];
function AuthenticationEvents($rootScope, $location, Authentication, Session, _, $route) {
$rootScope.currentUser = null;
$rootScope.loggedin = false;

Expand All @@ -23,6 +23,7 @@ function AuthenticationEvents($rootScope, $location, Authentication, Session, _)
if (redirect) {
$location.url(redirect);
}
$route.reload();
}

function doLogout(redirect) {
Expand All @@ -31,6 +32,7 @@ function AuthenticationEvents($rootScope, $location, Authentication, Session, _)
if (redirect) {
$location.url(redirect);
}
$route.reload();
}

// todo: move to service
Expand All @@ -54,11 +56,11 @@ function AuthenticationEvents($rootScope, $location, Authentication, Session, _)
};

$rootScope.$on('event:authentication:login:succeeded', function () {
doLogin(Session.getSessionDataEntry('loginPath') || '/');
doLogin(Session.getSessionDataEntry('loginPath'));
});

$rootScope.$on('event:authentication:logout:succeeded', function () {
doLogout('/');
doLogout();
});

// Don't think this is needed. We should already be logged out before this event
Expand All @@ -83,6 +85,8 @@ function AuthenticationEvents($rootScope, $location, Authentication, Session, _)
// We're logged out, redirect to login
if ($location.url() !== '/login') {
Session.setSessionDataEntry('loginPath', $location.url());
// We're logged in hit forbidden page
$location.url('/forbidden');
}
Authentication.openLogin();
}
Expand Down
19 changes: 12 additions & 7 deletions app/common/controllers/intercom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ module.exports = [
'ConfigEndpoint',
'$q',
'$window',
'$location',
function (
$scope,
$rootScope,
UserEndpoint,
ConfigEndpoint,
$q,
$window
$window,
$location
) {
var pattern = /^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)/g;
$rootScope.$on('event:authentication:login:succeeded', function () {
if ($window.self === $window.top) {
$scope.startIntercom();
Expand All @@ -28,19 +29,23 @@ function (
]).then(function (results) {
var site = results[0];
var user = results[1];
var domain = pattern.exec($window.ushahidi.apiUrl);
domain = domain[1].replace('api.', '');
var domain = $location.host();

$window.Intercom('boot', {
app_id: $window.ushahidi.intercomAppId,
email: user.email,
created_at: user.created,
user_id: domain + '_' + user.id,
'company': site.name,
'deployment_url': $window.ushahidi.apiUrl,
'deployment_url': domain,
'realname' : user.realname,
'last_login': user.last_login,
'role': user.role
'role': user.role,
company: {
name: site.name,
id: domain,
created_at: 0, // Faking this because we don't have this data
plan: site.tier
}
});
});
}
Expand Down
12 changes: 11 additions & 1 deletion app/main/posts/common/post-actions.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ PostActionsDirective.$inject = [
'PostEndpoint',
'Notify',
'$location',
'$route',
'PostActionsService'
];
function PostActionsDirective(
PostEndpoint,
Notify,
$location,
$route,
PostActionsService
) {
return {
Expand All @@ -34,7 +36,15 @@ function PostActionsDirective(

function deletePost() {
PostActionsService.delete($scope.post).then(function () {
$location.path('/views/list');
// If we're not already on some top level view
if ($location.path().indexOf('views') === -1 &&
$location.path().indexOf('collections') === -1 &&
$location.path().indexOf('savedsearches') === -1) {
// Redirect to list
$location.path('/views/list');
} else {
$route.reload();
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion app/main/posts/detail/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1 class="mode-context-title">{{form.name || 'post.unstructured.survey_title' |
<div class="post-band" style="background-color: {{form.color}};"></div>

<div class="listing-item-select">
<span class="status-indicator empty tooltip">
<span class="status-indicator tooltip" ng-class="{ empty: post.status!='draft', yellow: post.status=='draft'}">
<svg class="iconic">
<use xlink:href="../../img/iconic-sprite.svg#globe" ng-if="post.status=='published'"></use>
<use xlink:href="../../img/iconic-sprite.svg#lock-locked" ng-if="post.status=='draft'"></use>
Expand Down
4 changes: 2 additions & 2 deletions app/main/posts/detail/post-messages.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ <h2 class="listing-item-title">
</div>
</div>

<div class="listing-item">
<div class="listing-item" ng-show="totalItems > itemsPerPage">
<div class="listing-item-primary">

<uib-pagination ng-model="currentPage" items-per-page="itemsPerPage" total-items="totalItems" ng-change="pageChanged()" max-size="5" rotate="false"></pagination>

</div>
</div>

<div class="listing-item">
<div class="listing-item" ng-show="contact.type !== 'twitter'">
<div class="listing-item-primary">
<h2 class="listing-item-title">
<a href="" class="button button-flat" ng-click="reply()">
Expand Down
2 changes: 1 addition & 1 deletion app/main/posts/views/filters/active-filters.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div ng-repeat="(key, values) in activeFilters">
<span class="bug" ng-repeat="value in values" >
<span class="bug" ng-repeat="value in values track by $index" >
<svg class="iconic" ng-click="removeFilter(key, value)">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="../../img/iconic-sprite.svg#circle-x"></use>
</svg>
Expand Down
12 changes: 10 additions & 2 deletions app/main/posts/views/post-filters.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ function PostFiltersService(_, FormEndpoint) {
}

function setFilters(newState) {
// Replace 'all' with full list of statuses
// Gives less confusing active display, and works around API bug
if (newState.status === 'all') {
newState.status = ['published', 'draft', 'archived'];
}

// Replace filterState with defaults + newState
// Including defaults ensures all values are always defined
return angular.merge(filterState, getDefaults(), newState);
return angular.extend(filterState, getDefaults(), newState);
}

function clearFilters() {
Expand Down Expand Up @@ -113,7 +119,9 @@ function PostFiltersService(_, FormEndpoint) {
return true;
}
// Is an array with all the same elements? (order doesn't matter)
if (_.isArray(defaults[key]) && _.difference(value, defaults[key]).length === 0) {
if (_.isArray(defaults[key]) &&
_.difference(value, defaults[key]).length === 0 &&
_.difference(defaults[key], value).length === 0) {
return true;
}
// Is value empty? ..and not a date object
Expand Down
2 changes: 1 addition & 1 deletion app/settings/surveys/survey-editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ <h3 class="form-sheet-title" translate="survey.tasks">Tasks</h3>
<label translate="survey.task_description">
Task description
</label>
<textarea name="description" data-min-rows="1" rows="1" ng-model="task.descritpion"></textarea>
<textarea name="description" data-min-rows="1" rows="1" ng-model="task.description"></textarea>
</div>

<div class="form-field switch">
Expand Down
2 changes: 1 addition & 1 deletion app/settings/surveys/task-create.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<label for="task_description" translate="survey.task_description">
Task description
</label>
<textarea id="task_description" name="description" data-min-rows="1" rows="1" ng-model="newTask.descritpion"></textarea>
<textarea id="task_description" name="description" data-min-rows="1" rows="1" ng-model="newTask.description"></textarea>
</div>
<div class="form-field switch">
<label translate="survey.require_task">Require this task be completed before a post can be visible to the public</label>
Expand Down
18 changes: 12 additions & 6 deletions test/unit/common/auth/authentication-events.run.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ describe('global event handlers', function () {
mockedAuthenticationData,
mockedAuthenticationService,
$rootScope,
$location;
$location,
mockRoute = {
reload: jasmine.createSpy()
};

beforeEach(function () {

Expand Down Expand Up @@ -46,7 +49,10 @@ describe('global event handlers', function () {
.service('Authentication', function () {
return mockedAuthenticationService;
})
.run(require(rootPath + 'app/common/auth/authentication-events.run.js'));
.run(require(rootPath + 'app/common/auth/authentication-events.run.js'))
.service('$route', function () {
return mockRoute;
});

require(rootPath + 'test/unit/simple-test-app-config.js')(testApp);
});
Expand Down Expand Up @@ -92,8 +98,8 @@ describe('global event handlers', function () {
expect($rootScope.loggedin).toBe(true);
});

it('should change the path to "/"', function () {
expect($location.path()).toEqual('/');
it('should reload the route', function () {
expect(mockRoute.reload).toHaveBeenCalled();
});
});

Expand Down Expand Up @@ -129,8 +135,8 @@ describe('global event handlers', function () {
expect($rootScope.loggedin).toBe(false);
});

it('should change the path to "/"', function () {
expect($location.path()).toEqual('/');
it('should reload the route', function () {
expect(mockRoute.reload).toHaveBeenCalled();
});
});
});
Expand Down
36 changes: 32 additions & 4 deletions test/unit/main/post/common/post-actions.directive.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ describe('post actions directive', function () {

var $rootScope,
$scope,
$location,
isolateScope,
element,
PostActionsService,
PostEndpoint;
PostEndpoint,
mockRoute = {
reload: jasmine.createSpy()
};

beforeEach(function () {
fixture.setBase('mocked_backend/api/v3');
Expand All @@ -21,7 +25,8 @@ describe('post actions directive', function () {
testApp.directive('postActions', require(ROOT_PATH + 'app/main/posts/common/post-actions.directive'))
.value('$filter', function () {
return function () {};
});
})
.value('$route', mockRoute);

require(ROOT_PATH + 'test/unit/simple-test-app-config')(testApp);

Expand All @@ -30,12 +35,15 @@ describe('post actions directive', function () {

beforeEach(angular.mock.module('client-templates'));

beforeEach(inject(function (_$rootScope_, $compile, _PostEndpoint_, _PostActionsService_) {
beforeEach(inject(function (_$rootScope_, $compile, _PostEndpoint_, _PostActionsService_, _$location_) {
$rootScope = _$rootScope_;
PostEndpoint = _PostEndpoint_;
PostActionsService = _PostActionsService_;
$location = _$location_;

$scope = _$rootScope_.$new();
$scope.post = fixture.load('posts/120.json');

element = '<post-actions post="post"></post-actions>';
element = $compile(element)($scope);
$rootScope.$digest();
Expand All @@ -47,9 +55,29 @@ describe('post actions directive', function () {
});

it('should delete a post', function () {
spyOn(PostActionsService, 'delete').and.callThrough();
spyOn(PostActionsService, 'delete').and.returnValue({
then: function (cb) {
cb();
}
});
spyOn($location, 'path').and.returnValue('/views/list');

isolateScope.deletePost(isolateScope.post);
expect(PostActionsService.delete).toHaveBeenCalled();
expect(mockRoute.reload).toHaveBeenCalled();
});

it('should delete a post and redirect to list', function () {
spyOn(PostActionsService, 'delete').and.returnValue({
then: function (cb) {
cb();
}
});
spyOn($location, 'path').and.returnValue('/post/120');

isolateScope.deletePost(isolateScope.post);
expect(PostActionsService.delete).toHaveBeenCalled();
expect($location.path).toHaveBeenCalledWith('/views/list');
});

it('should update the status of a post', function () {
Expand Down
50 changes: 47 additions & 3 deletions test/unit/main/post/views/filters/post-filters.service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Post Filters Service', function () {
'pascalprecht.translate'
]);

testApp.service('PostFiltersService', require(ROOT_PATH + 'app/main/posts/views/post-filters.service.js'))
testApp.service('PostFilters', require(ROOT_PATH + 'app/main/posts/views/post-filters.service.js'))
.value('$filter', function () {
return function () {
return 'Feb 17, 2016';
Expand All @@ -40,9 +40,53 @@ describe('Post Filters Service', function () {
describe('test service functions', function () {

it('should return the active filters when an array filter differs from the default', function () {
PostFilters.filterState.tags.push('test');
PostFilters.setFilters({ tags : ['test']});
var filters = PostFilters.getActiveFilters(PostFilters.getFilters());
expect(filters.tags.length).toEqual(1);
expect(filters.tags).toEqual(['test']);
});

it('should return status filter if different from defaults', function () {
var filters = PostFilters.getActiveFilters({ status: ['draft'] });
expect(filters.status).toEqual(['draft']);

filters = PostFilters.getActiveFilters({ status: ['published', 'archived'] });
expect(filters.status).toEqual(['published', 'archived']);

filters = PostFilters.getActiveFilters({ status: ['published', 'draft'] });
expect(filters.status).toEqual(undefined);
});

it('setFilters should not duplicate draft status', function () {
var filters = PostFilters.setFilters({ status : ['draft']});
expect(filters.status).toEqual(['draft']);
});

it('setFilters should override existing values with defaults', function () {
PostFilters.setFilters({
tags : ['test'],
status : ['archived'],
form: [1]
});

var filters = PostFilters.setFilters({
status: ['archived', 'draft'],
tags: [1,3,4],
form: ['none']
});
expect(filters).toEqual({
q: '',
date_after: '',
date_before: '',
status: ['archived', 'draft'],
published_to: '',
center_point: '',
within_km: '1',
current_stage: [],
tags: [1,3,4],
form: ['none'],
set: [],
user: false
});
});

});
Expand Down

0 comments on commit ec638b2

Please sign in to comment.