Commit 3f2512ad authored by Julien Schröter's avatar Julien Schröter Committed by Frederik Wegner

Add repository function to update firebase-token in the database

parent 9f9b93ab
......@@ -15,6 +15,7 @@ type RefreshJWTQuery interface {
Insert(userID uint32, token string) (uint32, error)
Refresh(sessionID uint32, version uint32) (uint32, error)
GetLatestToken(userID uint32) (string, error)
UpdateFirebaseToken(sessionID uint32, newToken string) error
}
type MySQLRefreshJWTQuery struct{}
......@@ -126,3 +127,22 @@ func (MySQLRefreshJWTQuery) GetLatestToken(userID uint32) (string, error) {
return token.String, nil
}
// UpdateFirebaseToken updates the token value, representing the firebase device token,
// of the given session. This will overwrite the token either set during login or updated later.
// UpdateFirebaseToken cannot unset the firebase token (No empty tokens allowed).
func (MySQLRefreshJWTQuery) UpdateFirebaseToken(sessionID uint32, newToken string) error {
db, err := dbhandler.GetDBConnection()
if err != nil {
return fmt.Errorf("failed to get database connection instance: %v", err)
}
_, err = db.Exec("UPDATE `session` SET `token`=? WHERE `idsession`=?", newToken, sessionID)
if err != nil {
return fmt.Errorf("failed to update token: %v", err)
}
// No problems, everything went fine
return nil
}
......@@ -49,6 +49,8 @@ func (instance *mockSession) Refresh(sessionID uint32, version uint32) (uint32,
return 0, refreshjwt.ErrNoMatchingSession
}
session.version = version + 1
return version + 1, nil
}
......@@ -56,6 +58,22 @@ func (mockSession) GetLatestToken(userID uint32) (string, error) {
return "", errors.New("mock function not implemented")
}
func (instance *mockSession) UpdateFirebaseToken(sessionID uint32, newToken string) error {
if sessionID >= instance.nextID {
return refreshjwt.ErrNoMatchingSession
}
session := &instance.Sessions[sessionID-1]
if session.id != sessionID {
panic("session not at expected position")
}
session.token = newToken
return nil
}
func Test_patchRefreshJWT200(t *testing.T) {
repository := newSession()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, nil, repository, nil, nil, nil, nil)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment