summaryrefslogtreecommitdiffstats
path: root/models/org_test.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-12-12 23:57:56 +0100
commite68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch)
tree97775d6c13b0f416af55314eb6a89ef792474615 /models/org_test.go
parentInitial commit. (diff)
downloadforgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz
forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to '')
-rw-r--r--models/org_test.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/models/org_test.go b/models/org_test.go
new file mode 100644
index 0000000..bb5e524
--- /dev/null
+++ b/models/org_test.go
@@ -0,0 +1,62 @@
+// Copyright 2017 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package models
+
+import (
+ "testing"
+
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/organization"
+ "code.gitea.io/gitea/models/unittest"
+ user_model "code.gitea.io/gitea/models/user"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestUser_RemoveMember(t *testing.T) {
+ require.NoError(t, unittest.PrepareTestDatabase())
+ org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
+
+ // remove a user that is a member
+ unittest.AssertExistsAndLoadBean(t, &organization.OrgUser{UID: 4, OrgID: 3})
+ prevNumMembers := org.NumMembers
+ require.NoError(t, RemoveOrgUser(db.DefaultContext, org.ID, 4))
+ unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: 4, OrgID: 3})
+ org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
+ assert.Equal(t, prevNumMembers-1, org.NumMembers)
+
+ // remove a user that is not a member
+ unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: 5, OrgID: 3})
+ prevNumMembers = org.NumMembers
+ require.NoError(t, RemoveOrgUser(db.DefaultContext, org.ID, 5))
+ unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: 5, OrgID: 3})
+ org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
+ assert.Equal(t, prevNumMembers, org.NumMembers)
+
+ unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
+}
+
+func TestRemoveOrgUser(t *testing.T) {
+ require.NoError(t, unittest.PrepareTestDatabase())
+ testSuccess := func(orgID, userID int64) {
+ org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID})
+ expectedNumMembers := org.NumMembers
+ if unittest.BeanExists(t, &organization.OrgUser{OrgID: orgID, UID: userID}) {
+ expectedNumMembers--
+ }
+ require.NoError(t, RemoveOrgUser(db.DefaultContext, orgID, userID))
+ unittest.AssertNotExistsBean(t, &organization.OrgUser{OrgID: orgID, UID: userID})
+ org = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID})
+ assert.EqualValues(t, expectedNumMembers, org.NumMembers)
+ }
+ testSuccess(3, 4)
+ testSuccess(3, 4)
+
+ err := RemoveOrgUser(db.DefaultContext, 7, 5)
+ require.Error(t, err)
+ assert.True(t, organization.IsErrLastOrgOwner(err))
+ unittest.AssertExistsAndLoadBean(t, &organization.OrgUser{OrgID: 7, UID: 5})
+ unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
+}