|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-present Invertase Limited & Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this library except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +const jwt = require('jsonwebtoken'); |
| 19 | + |
| 20 | +const ID_LENGTH = 22; |
| 21 | +const PROJECT_ID = 448618578101; // this is "magic", it's the react-native-firebase-testing project ID |
| 22 | + |
| 23 | +describe('installations() modular', function () { |
| 24 | + describe('firebase v8 compatibility', function () { |
| 25 | + describe('getId()', function () { |
| 26 | + it('returns a valid installation id', async function () { |
| 27 | + const id = await firebase.installations().getId(); |
| 28 | + id.should.be.a.String(); |
| 29 | + id.length.should.be.equals(ID_LENGTH); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('getToken()', function () { |
| 34 | + it('returns a valid auth token with no arguments', async function () { |
| 35 | + const id = await firebase.installations().getId(); |
| 36 | + const token = await firebase.installations().getToken(); |
| 37 | + token.should.be.a.String(); |
| 38 | + token.should.not.equal(''); |
| 39 | + const decodedToken = jwt.decode(token); |
| 40 | + decodedToken.fid.should.equal(id); // fid == firebase installations id |
| 41 | + decodedToken.projectNumber.should.equal(PROJECT_ID); |
| 42 | + |
| 43 | + // token time is "Unix epoch time", which is in seconds vs javascript milliseconds |
| 44 | + if (decodedToken.exp < Math.round(new Date().getTime() / 1000)) { |
| 45 | + return Promise.reject( |
| 46 | + new Error('Token already expired: ' + JSON.stringify(decodedToken)), |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + const token2 = await firebase.installations().getToken(true); |
| 51 | + token2.should.be.a.String(); |
| 52 | + token2.should.not.equal(''); |
| 53 | + const decodedToken2 = jwt.decode(token2); |
| 54 | + decodedToken2.fid.should.equal(id); |
| 55 | + decodedToken2.projectNumber.should.equal(PROJECT_ID); |
| 56 | + |
| 57 | + // token time is "Unix epoch time", which is in seconds vs javascript milliseconds |
| 58 | + if (decodedToken.exp < Math.round(new Date().getTime() / 1000)) { |
| 59 | + return Promise.reject(new Error('Token already expired')); |
| 60 | + } |
| 61 | + (token === token2).should.be.false(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('delete()', function () { |
| 66 | + it('successfully deletes', async function () { |
| 67 | + const id = await firebase.installations().getId(); |
| 68 | + id.should.be.a.String(); |
| 69 | + id.length.should.be.equals(ID_LENGTH); |
| 70 | + await firebase.installations().delete(); |
| 71 | + |
| 72 | + // New id should be different |
| 73 | + const id2 = await firebase.installations().getId(); |
| 74 | + id2.should.be.a.String(); |
| 75 | + id2.length.should.be.equals(ID_LENGTH); |
| 76 | + (id === id2).should.be.false(); |
| 77 | + |
| 78 | + const token = await firebase.installations().getToken(false); |
| 79 | + const decodedToken = jwt.decode(token); |
| 80 | + decodedToken.fid.should.equal(id2); // fid == firebase installations id |
| 81 | + |
| 82 | + // token time is "Unix epoch time", which is in seconds vs javascript milliseconds |
| 83 | + decodedToken.projectNumber.should.equal(PROJECT_ID); |
| 84 | + if (decodedToken.exp < Math.round(new Date().getTime() / 1000)) { |
| 85 | + return Promise.reject(new Error('Token already expired')); |
| 86 | + } |
| 87 | + }); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('modular', function () { |
| 92 | + describe('getId()', function () { |
| 93 | + it('returns a valid installation id', async function () { |
| 94 | + const { getInstallations, getId } = installationsModular; |
| 95 | + const installations = getInstallations(); |
| 96 | + |
| 97 | + const id = await getId(installations); |
| 98 | + id.should.be.a.String(); |
| 99 | + id.length.should.be.equals(ID_LENGTH); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('getToken()', function () { |
| 104 | + it('returns a valid auth token with no arguments', async function () { |
| 105 | + const { getInstallations, getId, getToken } = installationsModular; |
| 106 | + const installations = getInstallations(); |
| 107 | + |
| 108 | + const id = await getId(installations); |
| 109 | + const token = await getToken(installations); |
| 110 | + token.should.be.a.String(); |
| 111 | + token.should.not.equal(''); |
| 112 | + const decodedToken = jwt.decode(token); |
| 113 | + decodedToken.fid.should.equal(id); // fid == firebase installations id |
| 114 | + decodedToken.projectNumber.should.equal(PROJECT_ID); |
| 115 | + |
| 116 | + // token time is "Unix epoch time", which is in seconds vs javascript milliseconds |
| 117 | + if (decodedToken.exp < Math.round(new Date().getTime() / 1000)) { |
| 118 | + return Promise.reject( |
| 119 | + new Error('Token already expired: ' + JSON.stringify(decodedToken)), |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + const token2 = await getToken(installations, true); |
| 124 | + token2.should.be.a.String(); |
| 125 | + token2.should.not.equal(''); |
| 126 | + const decodedToken2 = jwt.decode(token2); |
| 127 | + decodedToken2.fid.should.equal(id); |
| 128 | + decodedToken2.projectNumber.should.equal(PROJECT_ID); |
| 129 | + |
| 130 | + // token time is "Unix epoch time", which is in seconds vs javascript milliseconds |
| 131 | + if (decodedToken.exp < Math.round(new Date().getTime() / 1000)) { |
| 132 | + return Promise.reject(new Error('Token already expired')); |
| 133 | + } |
| 134 | + (token === token2).should.be.false(); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('deleteInstallations()', function () { |
| 139 | + it('successfully deletes', async function () { |
| 140 | + const { getInstallations, getId, getToken, deleteInstallations } = installationsModular; |
| 141 | + const installations = getInstallations(); |
| 142 | + |
| 143 | + const id = await getId(installations); |
| 144 | + id.should.be.a.String(); |
| 145 | + id.length.should.be.equals(ID_LENGTH); |
| 146 | + await deleteInstallations(installations); |
| 147 | + |
| 148 | + // New id should be different |
| 149 | + const id2 = await getId(installations); |
| 150 | + id2.should.be.a.String(); |
| 151 | + id2.length.should.be.equals(ID_LENGTH); |
| 152 | + (id === id2).should.be.false(); |
| 153 | + |
| 154 | + const token = await getToken(installations, false); |
| 155 | + const decodedToken = jwt.decode(token); |
| 156 | + decodedToken.fid.should.equal(id2); // fid == firebase installations id |
| 157 | + |
| 158 | + // token time is "Unix epoch time", which is in seconds vs javascript milliseconds |
| 159 | + decodedToken.projectNumber.should.equal(PROJECT_ID); |
| 160 | + if (decodedToken.exp < Math.round(new Date().getTime() / 1000)) { |
| 161 | + return Promise.reject(new Error('Token already expired')); |
| 162 | + } |
| 163 | + }); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments