Skip to content

Commit

Permalink
Merge c004904 into c4ac69f
Browse files Browse the repository at this point in the history
  • Loading branch information
idanen committed May 23, 2017
2 parents c4ac69f + c004904 commit 034bc96
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/auth/FirebaseAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
$updateEmail: this.updateEmail.bind(this),
$deleteUser: this.deleteUser.bind(this),
$sendPasswordResetEmail: this.sendPasswordResetEmail.bind(this),
$linkUser: this.linkUser.bind(this),

// Hack: needed for tests
_: this
Expand Down Expand Up @@ -334,6 +335,26 @@
}
},

/**
* Links the currently logged in user to a user with the given credentials.
*
* @param {string} email User's email
* @param {string} password User's password
* @return {Promise<firebase.User>} A promise fulfilled with the `firebase.User` object.
*/
linkUser: function(email, password) {
var user = this.getAuth(),
credential;

if (!user) {
throw new Error('No user to link to (user must login before linking)');
}

credential = firebase.auth.EmailAuthProvider.credential(email, password);

return user.link(credential);
},


/**
* Sends a password reset email to an email/password user.
Expand Down
89 changes: 89 additions & 0 deletions tests/unit/FirebaseAuth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,4 +681,93 @@ describe('FirebaseAuth',function(){
expect(result).toEqual('myResult');
});
});

describe('$linkUser()',function (){
it('should return a promise', function () {
var email = 'somebody@somewhere.com';
var password = 'myPass';
var credentials = {
provider: 'password',
uid: 'testUid',
link: jasmine.createSpy().and.returnValue(fakePromise())
};
spyOn(authService._, 'getAuth').and.returnValue(credentials);

expect(authService.$linkUser(email, password)).toBeAPromise();
});

it('should pass email and password to method on backing auth instance', function () {
var email = 'somebody@somewhere.com',
password = 'myPass';
var credentials = {
provider: 'password',
uid: 'testUid',
link: jasmine.createSpy().and.returnValue(fakePromise())
};
spyOn(authService._, 'getAuth').and.callFake(function () {
return credentials;
});

spyOn(firebase.auth.EmailAuthProvider, 'credential').and.returnValue(fakePromise());

authService.$linkUser(email, password);
expect(firebase.auth.EmailAuthProvider.credential).toHaveBeenCalledWith(email, password);
});

it('should call Firebase User\'s link method with email credentials', function () {
var email = 'somebody@somewhere.com',
password = 'myPass',
mockCredentials = 'mock credentials';
var fbUser = {
provider: 'password',
uid: 'testUid',
link: jasmine.createSpy()
};
spyOn(authService._, 'getAuth').and.callFake(function () {
return fbUser;
});

spyOn(firebase.auth.EmailAuthProvider, 'credential').and.returnValue(mockCredentials);

authService.$linkUser(email, password);

expect(fbUser.link).toHaveBeenCalledWith(mockCredentials);
});

it('should resolve promise with the user', function (done) {
var email = 'somebody@somewhere.com',
password = 'myPass',
mockCredentials = 'mock credentials';
var mockUser = {
provider: 'password',
uid: 'testUid',
link: jasmine.createSpy().and.callFake(function (aCredential) {
if (aCredential === mockCredentials) {
return Promise.resolve(mockUser);
}
return Promise.reject(new Error('Wrong mail / password'));
})
};
spyOn(authService._, 'getAuth').and.callFake(function () {
return mockUser;
});

spyOn(firebase.auth.EmailAuthProvider, 'credential').and.returnValue(mockCredentials);

authService.$linkUser(email, password)
.then(function (aUser) {
expect(aUser).toBe(mockUser);
done();
})
.catch(function (err) {
fail('failed due to an error in promise chain: ' + err.message);
});
});

it('should throw an error if there\'s no current user', function () {
spyOn(authService._, 'getAuth').and.returnValue(null);

expect(function () { authService.$linkUser('noone@nowhere.com', 'nevermind'); }).toThrow(new Error('No user to link to (user must login before linking)'));
});
});
});

0 comments on commit 034bc96

Please sign in to comment.