[#276] added support for linking external auths by provider id

This commit is contained in:
Gani Georgiev
2022-08-31 13:38:31 +03:00
parent 9fe94f5c7d
commit f5ff7193a9
33 changed files with 924 additions and 223 deletions
+99
View File
@@ -0,0 +1,99 @@
package daos
import (
"errors"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/models"
)
// ExternalAuthQuery returns a new ExternalAuth select query.
func (dao *Dao) ExternalAuthQuery() *dbx.SelectQuery {
return dao.ModelQuery(&models.ExternalAuth{})
}
/// FindAllExternalAuthsByUserId returns all ExternalAuth models
/// linked to the provided userId.
func (dao *Dao) FindAllExternalAuthsByUserId(userId string) ([]*models.ExternalAuth, error) {
auths := []*models.ExternalAuth{}
err := dao.ExternalAuthQuery().
AndWhere(dbx.HashExp{"userId": userId}).
OrderBy("created ASC").
All(&auths)
if err != nil {
return nil, err
}
return auths, nil
}
// FindExternalAuthByProvider returns the first available
// ExternalAuth model for the specified provider and providerId.
func (dao *Dao) FindExternalAuthByProvider(provider, providerId string) (*models.ExternalAuth, error) {
model := &models.ExternalAuth{}
err := dao.ExternalAuthQuery().
AndWhere(dbx.HashExp{
"provider": provider,
"providerId": providerId,
}).
Limit(1).
One(model)
if err != nil {
return nil, err
}
return model, nil
}
// FindExternalAuthByUserIdAndProvider returns the first available
// ExternalAuth model for the specified userId and provider.
func (dao *Dao) FindExternalAuthByUserIdAndProvider(userId, provider string) (*models.ExternalAuth, error) {
model := &models.ExternalAuth{}
err := dao.ExternalAuthQuery().
AndWhere(dbx.HashExp{
"userId": userId,
"provider": provider,
}).
Limit(1).
One(model)
if err != nil {
return nil, err
}
return model, nil
}
// SaveExternalAuth upserts the provided ExternalAuth model.
func (dao *Dao) SaveExternalAuth(model *models.ExternalAuth) error {
return dao.Save(model)
}
// DeleteExternalAuth deletes the provided ExternalAuth model.
//
// The delete may fail if the linked user doesn't have an email and
// there are no other linked ExternalAuth models available.
func (dao *Dao) DeleteExternalAuth(model *models.ExternalAuth) error {
user, err := dao.FindUserById(model.UserId)
if err != nil {
return err
}
if user.Email == "" {
allExternalAuths, err := dao.FindAllExternalAuthsByUserId(user.Id)
if err != nil {
return err
}
if len(allExternalAuths) <= 1 {
return errors.New("You cannot delete the only available external auth relation because the user doesn't have an email set.")
}
}
return dao.Delete(model)
}
+189
View File
@@ -0,0 +1,189 @@
package daos_test
import (
"testing"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tests"
)
func TestExternalAuthQuery(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
expected := "SELECT {{_externalAuths}}.* FROM `_externalAuths`"
sql := app.Dao().ExternalAuthQuery().Build().SQL()
if sql != expected {
t.Errorf("Expected sql %s, got %s", expected, sql)
}
}
func TestFindAllExternalAuthsByUserId(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
userId string
expectedCount int
}{
{"", 0},
{"missing", 0},
{"97cc3d3d-6ba2-383f-b42a-7bc84d27410c", 0},
{"cx9u0dh2udo8xol", 2},
}
for i, s := range scenarios {
auths, err := app.Dao().FindAllExternalAuthsByUserId(s.userId)
if err != nil {
t.Errorf("(%d) Unexpected error %v", i, err)
continue
}
if len(auths) != s.expectedCount {
t.Errorf("(%d) Expected %d auths, got %d", i, s.expectedCount, len(auths))
}
for _, auth := range auths {
if auth.UserId != s.userId {
t.Errorf("(%d) Expected all auths to be linked to userId %s, got %v", i, s.userId, auth)
}
}
}
}
func TestFindExternalAuthByProvider(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
provider string
providerId string
expectedId string
}{
{"", "", ""},
{"github", "", ""},
{"github", "id1", ""},
{"github", "id2", ""},
{"google", "id1", "abcdefghijklmn0"},
{"gitlab", "id2", "abcdefghijklmn1"},
}
for i, s := range scenarios {
auth, err := app.Dao().FindExternalAuthByProvider(s.provider, s.providerId)
hasErr := err != nil
expectErr := s.expectedId == ""
if hasErr != expectErr {
t.Errorf("(%d) Expected hasErr %v, got %v", i, expectErr, err)
continue
}
if auth != nil && auth.Id != s.expectedId {
t.Errorf("(%d) Expected external auth with ID %s, got \n%v", i, s.expectedId, auth)
}
}
}
func TestFindExternalAuthByUserIdAndProvider(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
userId string
provider string
expectedId string
}{
{"", "", ""},
{"", "github", ""},
{"123456", "github", ""}, // missing user and provider record
{"123456", "google", ""}, // missing user but existing provider record
{"97cc3d3d-6ba2-383f-b42a-7bc84d27410c", "google", ""},
{"cx9u0dh2udo8xol", "google", "abcdefghijklmn0"},
{"cx9u0dh2udo8xol", "gitlab", "abcdefghijklmn1"},
}
for i, s := range scenarios {
auth, err := app.Dao().FindExternalAuthByUserIdAndProvider(s.userId, s.provider)
hasErr := err != nil
expectErr := s.expectedId == ""
if hasErr != expectErr {
t.Errorf("(%d) Expected hasErr %v, got %v", i, expectErr, err)
continue
}
if auth != nil && auth.Id != s.expectedId {
t.Errorf("(%d) Expected external auth with ID %s, got \n%v", i, s.expectedId, auth)
}
}
}
func TestSaveExternalAuth(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
auth := &models.ExternalAuth{
UserId: "97cc3d3d-6ba2-383f-b42a-7bc84d27410c",
Provider: "test",
ProviderId: "test_id",
}
if err := app.Dao().SaveExternalAuth(auth); err != nil {
t.Fatal(err)
}
// check if it was really saved
foundAuth, err := app.Dao().FindExternalAuthByProvider("test", "test_id")
if err != nil {
t.Fatal(err)
}
if auth.Id != foundAuth.Id {
t.Fatalf("Expected ExternalAuth with id %s, got \n%v", auth.Id, foundAuth)
}
}
func TestDeleteExternalAuth(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
user, err := app.Dao().FindUserById("cx9u0dh2udo8xol")
if err != nil {
t.Fatal(err)
}
auths, err := app.Dao().FindAllExternalAuthsByUserId(user.Id)
if err != nil {
t.Fatal(err)
}
if err := app.Dao().DeleteExternalAuth(auths[0]); err != nil {
t.Fatalf("Failed to delete the first ExternalAuth relation, got \n%v", err)
}
if err := app.Dao().DeleteExternalAuth(auths[1]); err == nil {
t.Fatal("Expected delete to fail, got nil")
}
// update the user model and try again
user.Email = "test_new@example.com"
if err := app.Dao().SaveUser(user); err != nil {
t.Fatal(err)
}
// try to delete auths[1] again
if err := app.Dao().DeleteExternalAuth(auths[1]); err != nil {
t.Fatalf("Failed to delete the last ExternalAuth relation, got \n%v", err)
}
// check if the relations were really deleted
newAuths, err := app.Dao().FindAllExternalAuthsByUserId(user.Id)
if err != nil {
t.Fatal(err)
}
if len(newAuths) != 0 {
t.Fatalf("Expected all user %s ExternalAuth relations to be deleted, got \n%v", user.Id, newAuths)
}
}
+2 -1
View File
@@ -94,7 +94,7 @@ func (dao *Dao) FindUserById(id string) (*models.User, error) {
return model, nil
}
// FindUserByEmail finds a single User model by its email address.
// FindUserByEmail finds a single User model by its non-empty email address.
//
// This method also auto loads the related user profile record
// into the found model.
@@ -102,6 +102,7 @@ func (dao *Dao) FindUserByEmail(email string) (*models.User, error) {
model := &models.User{}
err := dao.UserQuery().
AndWhere(dbx.Not(dbx.HashExp{"email": ""})).
AndWhere(dbx.HashExp{"email": email}).
Limit(1).
One(model)
+1
View File
@@ -110,6 +110,7 @@ func TestFindUserByEmail(t *testing.T) {
email string
expectError bool
}{
{"", true},
{"invalid", true},
{"missing@example.com", true},
{"test@example.com", false},