From 87cfe8944133450e59e485f7e1ec496c62b0293b Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 16 Mar 2025 18:21:42 +0100 Subject: [PATCH] feat(ldap): add tests --- pkg/modules/auth/ldap/ldap_test.go | 74 ++++++++++++++++++++++++++ pkg/modules/auth/ldap/main_test.go | 37 +++++++++++++ pkg/modules/auth/openid/main_test.go | 1 - pkg/modules/auth/openid/openid_test.go | 1 + 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 pkg/modules/auth/ldap/ldap_test.go create mode 100644 pkg/modules/auth/ldap/main_test.go diff --git a/pkg/modules/auth/ldap/ldap_test.go b/pkg/modules/auth/ldap/ldap_test.go new file mode 100644 index 000000000..372ee2d02 --- /dev/null +++ b/pkg/modules/auth/ldap/ldap_test.go @@ -0,0 +1,74 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-present Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public Licensee as published by +// 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 +// GNU Affero General Public Licensee for more details. +// +// You should have received a copy of the GNU Affero General Public Licensee +// along with this program. If not, see . + +package ldap + +import ( + "os" + "testing" + + "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/db" + user2 "code.vikunja.io/api/pkg/user" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestLdapLogin(t *testing.T) { + if os.Getenv("VIKUNJA_TESTS_USE_CONFIG") != "1" || !config.AuthLdapEnabled.GetBool() { + t.Skip("Skipping LDAP tests because ldap is not configured") + } + + // We assume this ldap test server is used: https://gitea.com/gitea/test-openldap + + t.Run("should create account", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + s := db.NewSession() + defer s.Close() + + user, err := AuthenticateUserInLDAP(s, "professor", "professor") + + require.NoError(t, err) + assert.Equal(t, "professor", user.Username) + db.AssertExists(t, "users", map[string]interface{}{ + "username": "professor", + "issuer": "ldap", + }, false) + }) + + t.Run("should not create account for wrong password", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + s := db.NewSession() + defer s.Close() + + _, err := AuthenticateUserInLDAP(s, "professor", "wrongpassword") + + require.Error(t, err) + assert.True(t, user2.IsErrWrongUsernameOrPassword(err)) + }) + + t.Run("should not create account for wrong user", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + s := db.NewSession() + defer s.Close() + + _, err := AuthenticateUserInLDAP(s, "gnome", "professor") + + require.Error(t, err) + assert.True(t, user2.IsErrWrongUsernameOrPassword(err)) + }) +} diff --git a/pkg/modules/auth/ldap/main_test.go b/pkg/modules/auth/ldap/main_test.go new file mode 100644 index 000000000..cf8400e19 --- /dev/null +++ b/pkg/modules/auth/ldap/main_test.go @@ -0,0 +1,37 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-present Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public Licensee as published by +// 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 +// GNU Affero General Public Licensee for more details. +// +// You should have received a copy of the GNU Affero General Public Licensee +// along with this program. If not, see . + +package ldap + +import ( + "os" + "testing" + + "code.vikunja.io/api/pkg/events" + "code.vikunja.io/api/pkg/files" + "code.vikunja.io/api/pkg/models" + "code.vikunja.io/api/pkg/user" +) + +// TestMain is the main test function used to bootstrap the test env +func TestMain(m *testing.M) { + user.InitTests() + files.InitTests() + models.SetupTests() + events.Fake() + InitializeLDAPConnection() + os.Exit(m.Run()) +} diff --git a/pkg/modules/auth/openid/main_test.go b/pkg/modules/auth/openid/main_test.go index 9225d3299..39cd21350 100644 --- a/pkg/modules/auth/openid/main_test.go +++ b/pkg/modules/auth/openid/main_test.go @@ -21,7 +21,6 @@ import ( "testing" "code.vikunja.io/api/pkg/events" - "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" diff --git a/pkg/modules/auth/openid/openid_test.go b/pkg/modules/auth/openid/openid_test.go index c4e303e86..0c8185402 100644 --- a/pkg/modules/auth/openid/openid_test.go +++ b/pkg/modules/auth/openid/openid_test.go @@ -23,6 +23,7 @@ import ( "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" "code.vikunja.io/api/pkg/utils" + "github.com/coreos/go-oidc/v3/oidc" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"