Skip to content

Commit

Permalink
Fixed auth_token on media.
Browse files Browse the repository at this point in the history
Added unit tests that ensure Media appends auth tokens to urls.
Also fixed the ping functionality so the app now actually retrieves a token.
Also enhanced $url so that it converts QSPs into ruby friendly casing.
  • Loading branch information
atruskie committed Nov 29, 2014
1 parent 674e608 commit 8069254
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 168 deletions.
12 changes: 8 additions & 4 deletions src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ var app = angular.module('baw',
'bawApp.birdWalks'
])

.config(['$routeProvider', '$locationProvider', '$httpProvider', 'conf.paths', 'conf.constants', '$sceDelegateProvider', 'growlProvider', 'localStorageServiceProvider',
function ($routeProvider, $locationProvider, $httpProvider, paths, constants, $sceDelegateProvider, growlProvider, localStorageServiceProvider) {
.config(['$routeProvider', '$locationProvider', '$httpProvider', 'conf.paths', 'conf.constants', '$sceDelegateProvider', 'growlProvider', 'localStorageServiceProvider', "$urlProvider", "casingTransformers",
function ($routeProvider, $locationProvider, $httpProvider, paths, constants, $sceDelegateProvider, growlProvider, localStorageServiceProvider, $urlProvider, casingTransformers) {
// adjust security whitelist for resource urls
var currentWhitelist = $sceDelegateProvider.resourceUrlWhitelist();
currentWhitelist.push(paths.api.root+'/**');
Expand Down Expand Up @@ -193,11 +193,15 @@ var app = angular.module('baw',

// configure local storage provider with our own namepspace
localStorageServiceProvider.setPrefix(constants.namespace);

$urlProvider.renamer(function(key) {
return casingTransformers.underscore(key);
});
}])


.run(['$rootScope', '$location', '$route', '$http', 'AudioEvent', 'conf.paths', 'UserProfile', 'ngAudioEvents', '$url',
function ($rootScope, $location, $route, $http, AudioEvent, paths, UserProfile, ngAudioEvents, $url) {
.run(['$rootScope', '$location', '$route', '$http', 'Authenticator', 'AudioEvent', 'conf.paths', 'UserProfile', 'ngAudioEvents', '$url',
function ($rootScope, $location, $route, $http, Authenticator, AudioEvent, paths, UserProfile, ngAudioEvents, $url) {

// embed configuration for easy site-wide binding
$rootScope.paths = paths;
Expand Down
2 changes: 1 addition & 1 deletion src/app/home/home.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</p>

<p ng-show="loggedIn">
Welcome back to the Bioacoustic Workbench, <span>{{$root.userData.friendlyName}}</span>! We're glad you're
Welcome back to the Bioacoustic Workbench, <span>{{$root.userData.userName}}</span>! We're glad you're
here.
</p>

Expand Down
20 changes: 3 additions & 17 deletions src/app/login/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ angular.module('bawApp.login', [])
['$scope', '$http', '$location', 'authService', 'AuthenticationProviders', 'Authenticator',
function LoginCtrl($scope, $http, $location, authService, AuthenticationProviders, Authenticator) {

// WARNING: Cookies required for this to work
function checkLogin() {
Authenticator.checkLogin();
}

checkLogin();

$scope.submit = function (provider) {

var authProvider = AuthenticationProviders[provider];
Expand Down Expand Up @@ -50,6 +43,7 @@ angular.module('bawApp.login', [])

var provider, actualProvider;
try {
/**** BROKEN *88888******/
provider = $scope.$root.userData.providerId;
}
catch (e) {
Expand All @@ -70,17 +64,9 @@ angular.module('bawApp.login', [])
$scope.$emit('event:auth-loginCancelled');
};

$scope.displayName = "";
$scope.email = "";

$scope.$watch('$root.loggedIn', function () {
if ($scope.loggedIn) {
$scope.friendlyName = $scope.userData.friendlyName;
}
else {
$scope.friendlyName = "";
}
});



}
]);
Expand Down
6 changes: 3 additions & 3 deletions src/baw.configuration.tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ angular.module('bawApp.configuration', ['url'])
show: "/audio_recordings/{recordingId}/media.{format}"
},
security: {
signOut: "/security",
ping: "/security",
signIn: "/security"
signOut: "/security/user",
ping: "/security/user",
signIn: "/security/user"
},
user: {
profile: "/my_account",
Expand Down
148 changes: 82 additions & 66 deletions src/components/models/media.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,82 @@
angular.module("baw.models.media", []).factory("baw.models.Media", ["conf.paths", function(paths) {

function Media(resource) {
if (!(this instanceof Media)) {
throw new Error("Constructor called as a function");
}

if (!angular.isObject(resource)) {
throw "Media must be constructed with a valid resource.";
}

angular.extend(this, resource);

// convert the datetime
this.recording.recordedDate = new Date(this.recording.recordedDate);

// alias common parameters
this.startOffset = this.commonParameters.startOffset;
this.endOffset = this.commonParameters.endOffset;
this.recordedDate = this.recording.recordedDate;
this.id = this.recording.id;
this.durationSeconds = this.recording.durationSeconds;
if (angular.isNumber(resource.commonParameters.sampleRate)) {
this.sampleRate = resource.commonParameters.sampleRate;
}
else {
throw "The provided sample rate for the Media json must be a number!";
}


/**
* Change relative image and audio urls into absolute urls
* @param {Media=} mediaItemToFix
*/
this.formatPaths = function formatPaths(mediaItemToFix) {
var mediaItem = mediaItemToFix || this;
var imgKeys = Object.keys(mediaItem.available.image);
if (imgKeys.length > 1) {
throw "don't know how to handle more than one image format!";
}

var imageKey = imgKeys[0];
var imageFormat = mediaItem.available.image[imageKey];
mediaItem.available.image[imageKey].url = paths.joinFragments(paths.api.root, imageFormat.url);
mediaItem.spectrogram = imageFormat;

// make the order explicit (ng-repeat alphabetizes the order >:-|
mediaItem.available.audioOrder = [];
angular.forEach(mediaItem.available.audio, function (value, key) {
// just update the url so it is an absolute uri
this[key].url = paths.joinFragments(paths.api.root, value.url);

mediaItem.available.audioOrder.push(key);

}, mediaItem.available.audio);
};

this.formatPaths();
}

Media.make = function(arg) {
return new Media(arg);
};

return Media;
}]);
angular
.module("baw.models.media", ["bawApp.services"])
.factory(
"baw.models.Media",
["conf.paths",
"Authenticator",
"$url",
function (paths, Authenticator, url) {

function Media(resource) {
if (!(this instanceof Media)) {
throw new Error("Constructor called as a function");
}

if (!angular.isObject(resource)) {
throw "Media must be constructed with a valid resource.";
}

angular.extend(this, resource);

// convert the datetime
this.recording.recordedDate = new Date(this.recording.recordedDate);

// alias common parameters
this.startOffset = this.commonParameters.startOffset;
this.endOffset = this.commonParameters.endOffset;
this.recordedDate = this.recording.recordedDate;
this.id = this.recording.id;
this.durationSeconds = this.recording.durationSeconds;
if (angular.isNumber(resource.commonParameters.sampleRate)) {
this.sampleRate = resource.commonParameters.sampleRate;
}
else {
throw "The provided sample rate for the Media json must be a number!";
}


/**
* Change relative image and audio urls into absolute urls.
* Also appends auth tokens onto urls.
* @param {Media=} mediaItemToFix
*/
this.formatPaths = function formatPaths(mediaItemToFix) {
var mediaItem = mediaItemToFix || this;
var imgKeys = Object.keys(mediaItem.available.image);
if (imgKeys.length > 1) {
throw "don't know how to handle more than one image format!";
}

var imageKey = imgKeys[0];
var imageFormat = mediaItem.available.image[imageKey];
var fullUrl = paths.joinFragments(paths.api.root, imageFormat.url);
mediaItem.available.image[imageKey].url = url.formatUri(fullUrl, {userToken: Authenticator.authToken});

mediaItem.spectrogram = imageFormat;

// make the order explicit (ng-repeat alphabetizes the order >:-|
mediaItem.available.audioOrder = [];
angular.forEach(mediaItem.available.audio, function (value, key) {
// just update the url so it is an absolute uri
var fullUrl = paths.joinFragments(paths.api.root, value.url);

// also add auth token
this[key].url = url.formatUri(fullUrl, {userToken: Authenticator.authToken});

mediaItem.available.audioOrder.push(key);

}, mediaItem.available.audio);

var jsonFullUrl = paths.joinFragments(paths.api.root, mediaItem.available.text["json"].url);
mediaItem.available.text["json"].url = url.formatUri(jsonFullUrl, {userToken: Authenticator.authToken});
};

this.formatPaths();
}

Media.make = function (arg) {
return new Media(arg);
};

return Media;
}]);
29 changes: 28 additions & 1 deletion src/components/models/media.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@ describe("The Media object", function () {

beforeEach(function(){
module("rails");

module("url", function(casingTransformers, $urlProvider) {
$urlProvider.renamer(function (key) { return casingTransformers.underscore(key);});
});

module("rails");
module("http-auth-interceptor");
module("bawApp.services");
module("baw.models");
module(function ($provide) {
$provide.value("Authenticator", {authToken:"67tgfyb7i6tgyu"});
});
});

beforeEach(inject(["casingTransformers", "baw.models.Media", function (casingTransformers, _media) {
Expand All @@ -85,7 +96,7 @@ describe("The Media object", function () {
existingMedia = new Media(transformed.data);
}]));

it("should be found globally", function () {
it("should be not be found globally", function () {
var type = typeof Media;
expect(type).toEqual("function");
});
Expand All @@ -97,4 +108,20 @@ describe("The Media object", function () {
var instanceOfDate = datetime instanceof Date;
expect(instanceOfDate).toBeTrue();
});

it("will append the token onto all urls", function() {
var token = "user_token=67tgfyb7i6tgyu";
var keys = ["audio", "image", "text"];

function checkUrls(mediaType) {
var obj = existingMedia.available[mediaType];
angular.forEach(obj, function (value, mimeType) {
var tokenIncluded = value.url.indexOf(token) >= 0;

expect(tokenIncluded).toBeTrue("(url did not contain token - `" + value.url + "` )");
});
}

keys.map(checkUrls);
});
});
Loading

0 comments on commit 8069254

Please sign in to comment.