2020-11-21 16:38:58 +00:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 19:19:13 +00:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2020-11-21 16:38:58 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 15:41:52 +00:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2020-11-21 16:38:58 +00:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 15:41:52 +00:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2020-11-21 16:38:58 +00:00
|
|
|
//
|
2020-12-23 15:41:52 +00:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-11-21 16:38:58 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2020-11-26 02:25:57 +00:00
|
|
|
package auth
|
2020-11-21 16:38:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.vikunja.io/api/pkg/db"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetOrCreateUser(t *testing.T) {
|
|
|
|
t.Run("new user", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 15:32:28 +00:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-11-26 02:25:57 +00:00
|
|
|
u, err := GetOrCreateUserFromExternalAuth(s, "https://some.issuer", "12345", "test@example.com", "", "someUserWhoDoesNotExistYet")
|
2020-12-23 15:32:28 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
|
|
|
|
2020-11-21 16:38:58 +00:00
|
|
|
db.AssertExists(t, "users", map[string]interface{}{
|
|
|
|
"id": u.ID,
|
2020-11-26 02:25:57 +00:00
|
|
|
"email": "test@example.com",
|
2020-11-21 16:38:58 +00:00
|
|
|
"username": "someUserWhoDoesNotExistYet",
|
|
|
|
}, false)
|
|
|
|
})
|
|
|
|
t.Run("new user, no username provided", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 15:32:28 +00:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-11-26 02:25:57 +00:00
|
|
|
u, err := GetOrCreateUserFromExternalAuth(s, "https://some.issuer", "12345", "test@example.com", "", "")
|
2020-11-21 16:38:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, u.Username)
|
2020-12-23 15:32:28 +00:00
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-11-21 16:38:58 +00:00
|
|
|
db.AssertExists(t, "users", map[string]interface{}{
|
|
|
|
"id": u.ID,
|
2020-11-26 02:25:57 +00:00
|
|
|
"email": "test@example.com",
|
2020-11-21 16:38:58 +00:00
|
|
|
}, false)
|
|
|
|
})
|
|
|
|
t.Run("new user, no email address", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 15:32:28 +00:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-11-26 02:25:57 +00:00
|
|
|
_, err := GetOrCreateUserFromExternalAuth(s, "https://some.issuer", "12345", "", "", "")
|
2020-11-21 16:38:58 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("existing user, different email address", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 15:32:28 +00:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-11-26 02:25:57 +00:00
|
|
|
u, err := GetOrCreateUserFromExternalAuth(s, "https://some.service.com", "12345", "other-email-address@some.service.com", "", "")
|
2020-11-21 16:38:58 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-23 15:32:28 +00:00
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-11-21 16:38:58 +00:00
|
|
|
db.AssertExists(t, "users", map[string]interface{}{
|
|
|
|
"id": u.ID,
|
2020-11-26 02:25:57 +00:00
|
|
|
"email": "other-email-address@some.service.com",
|
2020-11-21 16:38:58 +00:00
|
|
|
}, false)
|
|
|
|
})
|
|
|
|
}
|