firebase / firebase/firebase-tools

Custom claims can't be set on a user made with makeUserRecord

Open
#3,083 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
4.5k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
84

Description

### Version info

**firebase-functions-test:** 0.2.3

**firebase-functions:** 3.13.1

**firebase-admin:** 9.4.2

### Test case

Create user with `makeUserRecord` and use that user in a test where custom claims are set.

### Steps to reproduce

#### custom claims function
``` typescript
const setCustomClaims = async (user: admin.auth.UserRecord) => {
const isAdmin = admins.includes(user.email!);
try {
functions.logger.info('Setting custom claims for', user.displayName);
await admin.auth().setCustomUserClaims(user.uid, {
admin: isAdmin,
});
} catch (e) {
functions.logger.error('Not able to assign custom claims', e);
}
};
```

#### unit test
``` javascript
it('tests a firestore write', async () => {
const wrapped = test.wrap(myFunctions.createUserRecord);
const user = test.auth.makeUserRecord({
uid: 'abc1234',
email: 'markgoho@gmail.com',
displayName: 'Mark Goho',
photoURL: 'https://example.com',
});

await wrapped(user);

const snap = await admin.firestore().doc('users/abc1234').get();
expect(snap.data()).to.eql({
admin: false,
email: 'markgoho@gmail.com',
displayName: 'Mark Goho',
photoUrl: 'https://example.com',
});
});
```

### Expected behavior

Custom claims can be set on test user.

### Actual behavior
from the `catch(e)` block above
``` bash
{"severity":"ERROR","message":"Not able to assign custom claims FirebaseAuthError: There is no user record corresponding to the provided identifier.
at FirebaseAuthError.FirebaseError [as constructor] (C:\\Users\\Mark\\Documents\\IdeaCrew\\active-branch-tracker\\functions\\node_modules\\firebase-admin\\lib\\utils\\error.js:44:28)
at FirebaseAuthError.PrefixedFirebaseError [as constructor] (C:\\Users\\Mark\\Documents\\IdeaCrew\\active-branch-tracker\\functions\\node_modules\\firebase-admin\\lib\\utils\\error.js:90:28)
at new FirebaseAuthError (C:\\Users\\Mark\\Documents\\IdeaCrew\\active-branch-tracker\\functions\\node_modules\\firebase-admin\\lib\\utils\\error.js:149:16)
at Function.FirebaseAuthError.fromServerError (C:\\Users\\Mark\\Documents\\IdeaCrew\\active-branch-tracker\\functions\\node_modules\\firebase-admin\\lib\\utils\\error.js:188:16)
at C:\\Users\\Mark\\Documents\\IdeaCrew\\active-branch-tracker\\functions\\node_modules\\firebase-admin\\lib\\auth\\auth-api-request.js:1526:49
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async setCustomClaims (C:\\Users\\Mark\\Documents\\IdeaCrew\\active-branch-tracker\\functions\\lib\\new-user\\new-user.js:19:9)
at async Object.createNewUser (C:\\Users\\Mark\\Documents\\IdeaCrew\\active-branch-tracker\\functions\\lib\\new-user\\new-user.js:9:5)
at async Function.run (C:\\Users\\Mark\\Documents\\IdeaCrew\\active-branch-tracker\\functions\\lib\\index.js:92:5)
at async Context. (C:\\Users\\Mark\\Documents\\IdeaCrew\\active-branch-tracker\\functions\\test\\functions.spec.js:50:5) {
errorInfo: {
code: 'auth/user-not-found',
message: 'There is no user record corresponding to the provided identifier.'
},
codePrefix: 'auth'
}"}
```
### Other relevant information

This cloud function _does work_ when triggered from a front end application (in this case Angular). This is a "real" user created via
![image](https://user-images.githubusercontent.com/9759954/105869831-a9fc1c80-5fc5-11eb-98cd-0f0e803503b9.png)

![image](https://user-images.githubusercontent.com/9759954/105870032-d9128e00-5fc5-11eb-9b3c-bfaa426bd8b1.png)

**The cloud function is working as intended** so this is appears to be an issue specifically with the unit test.

From the emulator logs:
``` bash
i functions: Beginning execution of "createUserRecord"
> {"uid":"zh1XghkAn3JWpufhjYVYPV26TiM4","email":"raccoon.mountain.447@example.com","emailVerified":true,"displayName":"Raccoon Mountain","photoURL":null,"phoneNumber":null,"disabled":false,"passwordHash":null,"passwordSalt":null,"tokensValidAfterTime":null,"metadata":{"creationTime":"1611675733645","lastSignInTime":"1611675733645"},"customClaims":{},"providerData":[{"providerId":"google.com","rawId":"5770689598097356708592827429612507515509","federatedId":"5770689598097356708592827429612507515509","displayName":"Raccoon Mountain","email":"raccoon.mountain.447@example.com","screenName":"mountain_raccoon"}]}
> {"severity":"INFO","message":"Setting custom claims for Raccoon Mountain"}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.