code.gitea.io/gitea@v1.21.7/models/organization/team_invite_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package organization_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  	"code.gitea.io/gitea/models/organization"
    11  	"code.gitea.io/gitea/models/unittest"
    12  	user_model "code.gitea.io/gitea/models/user"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestTeamInvite(t *testing.T) {
    18  	assert.NoError(t, unittest.PrepareTestDatabase())
    19  
    20  	team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2})
    21  
    22  	t.Run("MailExistsInTeam", func(t *testing.T) {
    23  		user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    24  
    25  		// user 2 already added to team 2, should result in error
    26  		_, err := organization.CreateTeamInvite(db.DefaultContext, user2, team, user2.Email)
    27  		assert.Error(t, err)
    28  	})
    29  
    30  	t.Run("CreateAndRemove", func(t *testing.T) {
    31  		user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
    32  
    33  		invite, err := organization.CreateTeamInvite(db.DefaultContext, user1, team, "org3@example.com")
    34  		assert.NotNil(t, invite)
    35  		assert.NoError(t, err)
    36  
    37  		// Shouldn't allow duplicate invite
    38  		_, err = organization.CreateTeamInvite(db.DefaultContext, user1, team, "org3@example.com")
    39  		assert.Error(t, err)
    40  
    41  		// should remove invite
    42  		assert.NoError(t, organization.RemoveInviteByID(db.DefaultContext, invite.ID, invite.TeamID))
    43  
    44  		// invite should not exist
    45  		_, err = organization.GetInviteByToken(db.DefaultContext, invite.Token)
    46  		assert.Error(t, err)
    47  	})
    48  }