Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. const functions = require('firebase-functions');
  2.  
  3. // The Firebase Admin SDK to access the Firebase Realtime Database.
  4. const admin = require('firebase-admin');
  5. admin.initializeApp();
  6.  
  7. // add new users to the database
  8. exports.account = functions.auth.user().onCreate(event => {
  9. // console.log('addAccount()', event);
  10. const { uid, providerData } = event;
  11. const email = providerData.email;
  12.  
  13. return admin
  14. .database()
  15. .ref('/users/' + uid)
  16. .set({ email, uid });
  17. });
  18.  
  19. const sinon = require('sinon');
  20. const chai = require('chai');
  21. const assert = chai.assert;
  22. const admin = require('firebase-admin');
  23. const test = require('firebase-functions-test')();
  24.  
  25. describe('Cloud Functions', () => {
  26. let myFunctions, adminInitStub, adminDatabaseStub;
  27.  
  28. before(() => {
  29. adminInitStub = sinon.stub(admin, 'initializeApp');
  30. adminDatabaseStub = sinon.stub(admin, 'database');
  31. myFunctions = require('../index');
  32. });
  33.  
  34. after(() => {
  35. adminInitStub.restore();
  36. adminDatabaseStub.restore();
  37. test.cleanup();
  38. });
  39.  
  40. describe('account', () => {
  41. it('should write to /users', () => {
  42. // given
  43. const setParam = { email: 'test@test.com', uid: 1234 };
  44.  
  45. const setStub = sinon.stub();
  46. setStub.withArgs(setParam).returns(true);
  47.  
  48. const refStub = sinon.stub();
  49. refStub.withArgs('/users/1234').returns({ set: setStub });
  50.  
  51. adminDatabaseStub.returns({ ref: refStub });
  52.  
  53. const wrapped = test.wrap(myFunctions.account);
  54.  
  55. const event = {
  56. uid: 1234,
  57. providerData: {
  58. email: 'test@test.com'
  59. }
  60. };
  61.  
  62. // when
  63. const actual = wrapped(event);
  64.  
  65. // then
  66. return assert.equal(actual, true);
  67. });
  68. });
  69. });
  70.  
  71. Cloud Functions
  72. account
  73. 1) should write to /users
  74.  
  75.  
  76. 0 passing (53ms)
  77. 1 failing
  78.  
  79. 1) Cloud Functions
  80. account
  81. should write to /users:
  82. Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
  83. at FirebaseAppError.FirebaseError [as constructor] (node_modules/firebase-admin/lib/utils/error.js:42:28)
  84. at FirebaseAppError.PrefixedFirebaseError [as constructor] (node_modules/firebase-admin/lib/utils/error.js:88:28)
  85. at new FirebaseAppError (node_modules/firebase-admin/lib/utils/error.js:122:28)
  86. at FirebaseNamespaceInternals.app (node_modules/firebase-admin/lib/firebase-namespace.js:101:19)
  87. at FirebaseNamespace.app (node_modules/firebase-admin/lib/firebase-namespace.js:402:30)
  88. at FirebaseNamespace.ensureApp (node_modules/firebase-admin/lib/firebase-namespace.js:418:24)
  89. at FirebaseNamespace.fn (node_modules/firebase-admin/lib/firebase-namespace.js:280:30)
  90. at Function.exports.account.functions.auth.user.onCreate.event [as run] (index.js:65:21)
  91. at wrapped (node_modules/firebase-functions-test/lib/main.js:68:30)
  92. at Context.it (test/index.test.js:45:22)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement