From 822e129a37773d1452b642e9b6ca4efaabcf02c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Tisza?= Date: Fri, 28 Feb 2014 02:45:02 +0000 Subject: [PATCH] Create fake user when userinfo request fails Quick and dirty fix for a bug that completely breaks the metadata panel. Should be refactored later (for a ForeignDbRepo, a missing user is normal, for anything else it should still be treated as an error condition, although maybe not with a rejection). Bug: 62019 Change-Id: I433ae2bd1334593d9c5bfe0272ce7207f3fdf723 Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/267 --- .../mmv/provider/mmv.provider.UserInfo.js | 7 ++-- .../provider/mmv.provider.UserInfo.test.js | 39 ++++++++++++++++--- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/resources/mmv/provider/mmv.provider.UserInfo.js b/resources/mmv/provider/mmv.provider.UserInfo.js index c5102c5ec..05030444c 100644 --- a/resources/mmv/provider/mmv.provider.UserInfo.js +++ b/resources/mmv/provider/mmv.provider.UserInfo.js @@ -15,7 +15,7 @@ * along with MultimediaViewer. If not, see . */ -( function ( mw, oo, $ ) { +( function ( mw, oo ) { /** * Gets user information (currently just the gender). @@ -65,7 +65,8 @@ if ( users[0] && users[0].name && users[0].gender ) { return new mw.mmv.model.User( users[0].name, users[0].gender ); } else { - return $.Deferred().reject( 'error in provider, user info not found' ); + mw.log( 'user info not found for ' + username + ' at ' + repoInfo.name); + return new mw.mmv.model.User( username, mw.mmv.model.User.Gender.UNKNOWN ); } } ); } @@ -74,4 +75,4 @@ }; mw.mmv.provider.UserInfo = UserInfo; -}( mediaWiki, OO, jQuery ) ); +}( mediaWiki, OO ) ); diff --git a/tests/qunit/mmv/provider/mmv.provider.UserInfo.test.js b/tests/qunit/mmv/provider/mmv.provider.UserInfo.test.js index 72f9b1932..07362c558 100644 --- a/tests/qunit/mmv/provider/mmv.provider.UserInfo.test.js +++ b/tests/qunit/mmv/provider/mmv.provider.UserInfo.test.js @@ -81,7 +81,7 @@ } ); } ); - QUnit.asyncTest( 'UserInfo missing user test', 1, function ( assert ) { + QUnit.asyncTest( 'UserInfo missing user test', 2, function ( assert ) { var api = { get: function() { return $.Deferred().resolve( { query: { @@ -93,13 +93,14 @@ repoInfo = {}, userInfoProvider = new mw.mmv.provider.UserInfo( api ); - userInfoProvider.get( username, repoInfo ).fail( function() { - assert.ok( true, 'promise rejected when user is missing' ); + userInfoProvider.get( username, repoInfo ).done( function( user ) { + assert.strictEqual( user.username, 'Catrope', 'username is set correctly' ); + assert.strictEqual( user.gender, mw.mmv.model.User.Gender.UNKNOWN, 'gender is set to unknown' ); QUnit.start(); } ); } ); - QUnit.asyncTest( 'UserInfo missing gender test', 1, function ( assert ) { + QUnit.asyncTest( 'UserInfo missing gender test', 2, function ( assert ) { var api = { get: function() { return $.Deferred().resolve( { query: { @@ -116,8 +117,34 @@ repoInfo = {}, userInfoProvider = new mw.mmv.provider.UserInfo( api ); - userInfoProvider.get( username, repoInfo ).fail( function() { - assert.ok( true, 'promise rejected when gender is missing' ); + userInfoProvider.get( username, repoInfo ).done( function( user ) { + assert.strictEqual( user.username, 'Catrope', 'username is set correctly' ); + assert.strictEqual( user.gender, mw.mmv.model.User.Gender.UNKNOWN, 'gender is set to unknown' ); + QUnit.start(); + } ); + } ); + + // this can happen when we get the image from a ForeignDBRepo + QUnit.asyncTest( 'UserInfo non-existant user test', 2, function ( assert ) { + var api = { get: function() { + return $.Deferred().resolve( { + query: { + users: [ + { + missing: '', + name: 'Catrope' + } + ] + } + } ); + } }, + username = 'Catrope', + repoInfo = {}, + userInfoProvider = new mw.mmv.provider.UserInfo( api ); + + userInfoProvider.get( username, repoInfo ).done( function( user ) { + assert.strictEqual( user.username, 'Catrope', 'username is set correctly' ); + assert.strictEqual( user.gender, mw.mmv.model.User.Gender.UNKNOWN, 'gender is set to unknown' ); QUnit.start(); } ); } );