Derive chat display-name from upstream HTTP user header (#1633)

This commit is contained in:
Fabio Huser 2021-12-31 06:33:45 +01:00 committed by GitHub
parent b2cd174676
commit 3963777660
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View file

@ -58,6 +58,10 @@ func RegisterAnonymousChatUser(w http.ResponseWriter, r *http.Request) {
// this is fine. register a new user anyway. // this is fine. register a new user anyway.
} }
if request.DisplayName == "" {
request.DisplayName = r.Header.Get("X-Forwarded-User")
}
newUser, err := user.CreateAnonymousUser(request.DisplayName) newUser, err := user.CreateAnonymousUser(request.DisplayName)
if err != nil { if err != nil {
WriteSimpleResponse(w, false, err.Error()) WriteSimpleResponse(w, false, err.Error())

View file

@ -38,3 +38,24 @@ test('can fetch chat messages', async (done) => {
done(); done();
}); });
test('can derive display name from user header', async (done) => {
const res = await request
.post('/api/chat/register')
.set('X-Forwarded-User', 'test-user')
.expect(200);
expect(res.body.displayName).toBe('test-user');
done();
});
test('can overwrite user header derived display name with body', async (done) => {
const res = await request
.post('/api/chat/register')
.send({displayName: 'TestUserChat'})
.set('X-Forwarded-User', 'test-user')
.expect(200);
expect(res.body.displayName).toBe('TestUserChat');
done();
});