mirror of https://github.com/gogs/gogs.git
Use `OrganizationsStore.Create` replace tests hack
parent
371c56e306
commit
e0a14a54fe
|
@ -420,10 +420,10 @@ func DeleteTeam(t *Team) error {
|
|||
|
||||
// TeamUser represents an team-user relation.
|
||||
type TeamUser struct {
|
||||
ID int64
|
||||
OrgID int64 `xorm:"INDEX"`
|
||||
TeamID int64 `xorm:"UNIQUE(s)"`
|
||||
UID int64 `xorm:"UNIQUE(s)"`
|
||||
ID int64 `gorm:"primaryKey"`
|
||||
OrgID int64 `xorm:"INDEX" gorm:"index"`
|
||||
TeamID int64 `xorm:"UNIQUE(s)" gorm:"uniqueIndex:team_user_unique"`
|
||||
UID int64 `xorm:"UNIQUE(s)" gorm:"uniqueIndex:team_user_unique"`
|
||||
}
|
||||
|
||||
func isTeamMember(e Engine, orgID, teamID, uid int64) bool {
|
||||
|
|
|
@ -386,6 +386,7 @@ func (db *organizations) List(ctx context.Context, opts ListOrganizationsOptions
|
|||
|
||||
type CreateOrganizationOptions struct {
|
||||
FullName string
|
||||
Email string
|
||||
Location string
|
||||
Website string
|
||||
Description string
|
||||
|
@ -411,7 +412,7 @@ func (db *organizations) Create(ctx context.Context, name string, ownerID int64,
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if Users.IsUsernameUsed(ctx, name, 0) {
|
||||
if NewUsersStore(db.DB).IsUsernameUsed(ctx, name, 0) {
|
||||
return nil, ErrOrganizationAlreadyExist{
|
||||
args: errutil.Args{
|
||||
"name": name,
|
||||
|
@ -423,6 +424,7 @@ func (db *organizations) Create(ctx context.Context, name string, ownerID int64,
|
|||
LowerName: strings.ToLower(name),
|
||||
Name: name,
|
||||
FullName: opts.FullName,
|
||||
Email: opts.Email,
|
||||
Type: UserTypeOrganization,
|
||||
Location: opts.Location,
|
||||
Website: opts.Website,
|
||||
|
|
|
@ -6,13 +6,15 @@ package db
|
|||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"gogs.io/gogs/internal/conf"
|
||||
"gogs.io/gogs/internal/dbtest"
|
||||
"gogs.io/gogs/internal/dbutil"
|
||||
)
|
||||
|
||||
func TestOrgs(t *testing.T) {
|
||||
|
@ -22,7 +24,7 @@ func TestOrgs(t *testing.T) {
|
|||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
tables := []any{new(User), new(EmailAddress), new(OrgUser)}
|
||||
tables := []any{new(User), new(EmailAddress), new(OrgUser), new(Team), new(TeamUser)}
|
||||
db := &organizations{
|
||||
DB: dbtest.NewDB(t, "orgs", tables...),
|
||||
}
|
||||
|
@ -55,23 +57,18 @@ func orgsList(t *testing.T, ctx context.Context, db *organizations) {
|
|||
bob, err := usersStore.Create(ctx, "bob", "bob@example.com", CreateUserOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
// TODO: Use Orgs.Create to replace SQL hack when the method is available.
|
||||
org1, err := usersStore.Create(ctx, "org1", "org1@example.com", CreateUserOptions{})
|
||||
require.NoError(t, err)
|
||||
org2, err := usersStore.Create(ctx, "org2", "org2@example.com", CreateUserOptions{})
|
||||
require.NoError(t, err)
|
||||
err = db.Exec(
|
||||
dbutil.Quote("UPDATE %s SET type = ? WHERE id IN (?, ?)", "user"),
|
||||
UserTypeOrganization, org1.ID, org2.ID,
|
||||
).Error
|
||||
require.NoError(t, err)
|
||||
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
|
||||
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
|
||||
|
||||
// TODO: Use Orgs.Join to replace SQL hack when the method is available.
|
||||
err = db.Exec(`INSERT INTO org_user (uid, org_id, is_public) VALUES (?, ?, ?)`, alice.ID, org1.ID, false).Error
|
||||
org1, err := db.Create(ctx, "org1", alice.ID, CreateOrganizationOptions{})
|
||||
require.NoError(t, err)
|
||||
err = db.Exec(`INSERT INTO org_user (uid, org_id, is_public) VALUES (?, ?, ?)`, alice.ID, org2.ID, true).Error
|
||||
org2, err := db.Create(ctx, "org2", alice.ID, CreateOrganizationOptions{})
|
||||
require.NoError(t, err)
|
||||
err = db.Exec(`INSERT INTO org_user (uid, org_id, is_public) VALUES (?, ?, ?)`, bob.ID, org2.ID, true).Error
|
||||
err = db.SetMemberVisibility(ctx, org2.ID, alice.ID, true)
|
||||
require.NoError(t, err)
|
||||
err = db.AddMember(ctx, org2.ID, bob.ID)
|
||||
require.NoError(t, err)
|
||||
err = db.SetMemberVisibility(ctx, org2.ID, alice.ID, true)
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
|
@ -119,16 +116,12 @@ func orgsList(t *testing.T, ctx context.Context, db *organizations) {
|
|||
}
|
||||
|
||||
func orgsSearchByName(t *testing.T, ctx context.Context, db *organizations) {
|
||||
// TODO: Use Orgs.Create to replace SQL hack when the method is available.
|
||||
usersStore := NewUsersStore(db.DB)
|
||||
org1, err := usersStore.Create(ctx, "org1", "org1@example.com", CreateUserOptions{FullName: "Acme Corp"})
|
||||
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
|
||||
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
|
||||
|
||||
org1, err := db.Create(ctx, "org1", 1, CreateOrganizationOptions{FullName: "Acme Corp"})
|
||||
require.NoError(t, err)
|
||||
org2, err := usersStore.Create(ctx, "org2", "org2@example.com", CreateUserOptions{FullName: "Acme Corp 2"})
|
||||
require.NoError(t, err)
|
||||
err = db.Exec(
|
||||
dbutil.Quote("UPDATE %s SET type = ? WHERE id IN (?, ?)", "user"),
|
||||
UserTypeOrganization, org1.ID, org2.ID,
|
||||
).Error
|
||||
org2, err := db.Create(ctx, "org2", 1, CreateOrganizationOptions{FullName: "Acme Corp 2"})
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("search for username org1", func(t *testing.T) {
|
||||
|
|
|
@ -20,7 +20,6 @@ import (
|
|||
"gogs.io/gogs/internal/auth"
|
||||
"gogs.io/gogs/internal/conf"
|
||||
"gogs.io/gogs/internal/dbtest"
|
||||
"gogs.io/gogs/internal/dbutil"
|
||||
"gogs.io/gogs/internal/errutil"
|
||||
"gogs.io/gogs/internal/osutil"
|
||||
"gogs.io/gogs/internal/repoutil"
|
||||
|
@ -88,7 +87,7 @@ func TestUsers(t *testing.T) {
|
|||
tables := []any{
|
||||
new(User), new(EmailAddress), new(Repository), new(Follow), new(PullRequest), new(PublicKey), new(OrgUser),
|
||||
new(Watch), new(Star), new(Issue), new(AccessToken), new(Collaboration), new(Action), new(IssueUser),
|
||||
new(Access),
|
||||
new(Access), new(Team), new(TeamUser),
|
||||
}
|
||||
db := &users{
|
||||
DB: dbtest.NewDB(t, "users", tables...),
|
||||
|
@ -375,15 +374,13 @@ func usersCount(t *testing.T, ctx context.Context, db *users) {
|
|||
got = db.Count(ctx)
|
||||
assert.Equal(t, int64(1), got)
|
||||
|
||||
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
|
||||
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
|
||||
|
||||
// Create an organization shouldn't count
|
||||
// TODO: Use Orgs.Create to replace SQL hack when the method is available.
|
||||
org1, err := db.Create(ctx, "org1", "org1@example.com", CreateUserOptions{})
|
||||
require.NoError(t, err)
|
||||
err = db.Exec(
|
||||
dbutil.Quote("UPDATE %s SET type = ? WHERE id = ?", "user"),
|
||||
UserTypeOrganization, org1.ID,
|
||||
).Error
|
||||
_, err = NewOrganizationsStore(db.DB).Create(ctx, "org1", 1, CreateOrganizationOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
got = db.Count(ctx)
|
||||
assert.Equal(t, int64(1), got)
|
||||
}
|
||||
|
@ -489,17 +486,10 @@ func usersDeleteByID(t *testing.T, ctx context.Context, db *users) {
|
|||
bob, err := db.Create(ctx, "bob", "bob@exmaple.com", CreateUserOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
// TODO: Use Orgs.Create to replace SQL hack when the method is available.
|
||||
org1, err := db.Create(ctx, "org1", "org1@example.com", CreateUserOptions{})
|
||||
require.NoError(t, err)
|
||||
err = db.Exec(
|
||||
dbutil.Quote("UPDATE %s SET type = ? WHERE id IN (?)", "user"),
|
||||
UserTypeOrganization, org1.ID,
|
||||
).Error
|
||||
require.NoError(t, err)
|
||||
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
|
||||
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
|
||||
|
||||
// TODO: Use Orgs.Join to replace SQL hack when the method is available.
|
||||
err = db.Exec(`INSERT INTO org_user (uid, org_id) VALUES (?, ?)`, bob.ID, org1.ID).Error
|
||||
_, err = NewOrganizationsStore(db.DB).Create(ctx, "org1", bob.ID, CreateOrganizationOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = db.DeleteByID(ctx, bob.ID, false)
|
||||
|
@ -691,16 +681,11 @@ func usersDeleteInactivated(t *testing.T, ctx context.Context, db *users) {
|
|||
// User with organization membership should be skipped
|
||||
bob, err := db.Create(ctx, "bob", "bob@exmaple.com", CreateUserOptions{})
|
||||
require.NoError(t, err)
|
||||
// TODO: Use Orgs.Create to replace SQL hack when the method is available.
|
||||
org1, err := db.Create(ctx, "org1", "org1@example.com", CreateUserOptions{})
|
||||
require.NoError(t, err)
|
||||
err = db.Exec(
|
||||
dbutil.Quote("UPDATE %s SET type = ? WHERE id IN (?)", "user"),
|
||||
UserTypeOrganization, org1.ID,
|
||||
).Error
|
||||
require.NoError(t, err)
|
||||
// TODO: Use Orgs.Join to replace SQL hack when the method is available.
|
||||
err = db.Exec(`INSERT INTO org_user (uid, org_id) VALUES (?, ?)`, bob.ID, org1.ID).Error
|
||||
|
||||
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
|
||||
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
|
||||
|
||||
_, err = NewOrganizationsStore(db.DB).Create(ctx, "org1", bob.ID, CreateOrganizationOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
// User activated state should be skipped
|
||||
|
@ -734,11 +719,10 @@ func usersGetByEmail(t *testing.T, ctx context.Context, db *users) {
|
|||
})
|
||||
|
||||
t.Run("ignore organization", func(t *testing.T) {
|
||||
// TODO: Use Orgs.Create to replace SQL hack when the method is available.
|
||||
org, err := db.Create(ctx, "gogs", "gogs@exmaple.com", CreateUserOptions{})
|
||||
require.NoError(t, err)
|
||||
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
|
||||
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
|
||||
|
||||
err = db.Model(&User{}).Where("id", org.ID).UpdateColumn("type", UserTypeOrganization).Error
|
||||
org, err := NewOrganizationsStore(db.DB).Create(ctx, "gogs", 1, CreateOrganizationOptions{Email: "gogs@example.com"})
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = db.GetByEmail(ctx, org.Email)
|
||||
|
@ -815,7 +799,15 @@ func usersGetByUsername(t *testing.T, ctx context.Context, db *users) {
|
|||
})
|
||||
|
||||
t.Run("wrong user type", func(t *testing.T) {
|
||||
// org1,err:=NewOrganizationsStore(db.DB).Create(ctx,"org1","// TODO: Use Orgs.Create
|
||||
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
|
||||
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
|
||||
|
||||
org1, err := NewOrganizationsStore(db.DB).Create(ctx, "org1", 1, CreateOrganizationOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = db.GetByUsername(ctx, org1.Name)
|
||||
wantErr := ErrUserNotExist{args: errutil.Args{"name": org1.Name}}
|
||||
assert.Equal(t, wantErr, err)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -915,13 +907,10 @@ func usersList(t *testing.T, ctx context.Context, db *users) {
|
|||
require.NoError(t, err)
|
||||
|
||||
// Create an organization shouldn't count
|
||||
// TODO: Use Orgs.Create to replace SQL hack when the method is available.
|
||||
org1, err := db.Create(ctx, "org1", "org1@example.com", CreateUserOptions{})
|
||||
require.NoError(t, err)
|
||||
err = db.Exec(
|
||||
dbutil.Quote("UPDATE %s SET type = ? WHERE id = ?", "user"),
|
||||
UserTypeOrganization, org1.ID,
|
||||
).Error
|
||||
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
|
||||
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
|
||||
|
||||
_, err = NewOrganizationsStore(db.DB).Create(ctx, "org1", bob.ID, CreateOrganizationOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
got, err := db.List(ctx, 1, 1)
|
||||
|
|
Loading…
Reference in New Issue