orgsCreate

jc/db-migrate-orgs
Joe Chen 2023-11-05 10:25:06 -05:00
parent b49e4a4089
commit ed260cbcb9
No known key found for this signature in database
GPG Key ID: 0BDE5280C552FF60
2 changed files with 84 additions and 17 deletions

View File

@ -9,15 +9,17 @@ import (
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/dbtest"
"gogs.io/gogs/internal/errutil"
)
func TestOrgs(t *testing.T) {
func TestOrganizations(t *testing.T) {
if testing.Short() {
t.Skip()
}
@ -33,6 +35,7 @@ func TestOrgs(t *testing.T) {
name string
test func(t *testing.T, ctx context.Context, db *organizations)
}{
{"Create", orgsCreate},
{"List", orgsList},
{"SearchByName", orgsSearchByName},
{"CountByUser", orgsCountByUser},
@ -50,6 +53,65 @@ func TestOrgs(t *testing.T) {
}
}
func orgsCreate(t *testing.T, ctx context.Context, db *organizations) {
usersStore := NewUsersStore(db.DB)
alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
require.NoError(t, err)
t.Run("name not allowed", func(t *testing.T) {
_, err := db.Create(ctx, "-", alice.ID, CreateOrganizationOptions{})
wantErr := ErrNameNotAllowed{
args: errutil.Args{
"reason": "reserved",
"name": "-",
},
}
assert.Equal(t, wantErr, err)
})
// Users and organizations share the same namespace for names.
t.Run("name already exists", func(t *testing.T) {
_, err := db.Create(ctx, alice.Name, alice.ID, CreateOrganizationOptions{})
wantErr := ErrOrganizationAlreadyExist{
args: errutil.Args{
"name": alice.Name,
},
}
assert.Equal(t, wantErr, err)
})
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsCreate-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
org, err := db.Create(
ctx,
"acme",
alice.ID,
CreateOrganizationOptions{
FullName: "Acme Corp",
Email: "admin@acme.com",
Location: "Earth",
Website: "acme.com",
Description: "A popcorn company",
},
)
require.NoError(t, err)
got, err := db.GetByName(ctx, org.Name)
require.NoError(t, err)
assert.Equal(t, org.Name, got.Name)
assert.Equal(t, org.FullName, got.FullName)
assert.Equal(t, org.Email, got.Email)
assert.Equal(t, org.Location, got.Location)
assert.Equal(t, org.Website, got.Website)
assert.Equal(t, org.Description, got.Description)
assert.Equal(t, -1, got.MaxRepoCreation)
assert.Equal(t, 1, got.NumTeams)
assert.Equal(t, 1, got.NumMembers)
assert.Equal(t, db.NowFunc().Format(time.RFC3339), got.Created.UTC().Format(time.RFC3339))
assert.Equal(t, db.NowFunc().Format(time.RFC3339), got.Updated.UTC().Format(time.RFC3339))
}
func orgsList(t *testing.T, ctx context.Context, db *organizations) {
usersStore := NewUsersStore(db.DB)
alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
@ -116,7 +178,7 @@ func orgsList(t *testing.T, ctx context.Context, db *organizations) {
}
func orgsSearchByName(t *testing.T, ctx context.Context, db *organizations) {
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsSearchByName-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
org1, err := db.Create(ctx, "org1", 1, CreateOrganizationOptions{FullName: "Acme Corp"})
@ -163,7 +225,7 @@ func orgsCountByUser(t *testing.T, ctx context.Context, db *organizations) {
bob, err := usersStore.Create(ctx, "bob", "bob@example.com", CreateUserOptions{})
require.NoError(t, err)
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsCountByUser-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
org1, err := db.Create(ctx, "org1", alice.ID, CreateOrganizationOptions{})

View File

@ -374,7 +374,7 @@ 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")
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "usersCount-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
// Create an organization shouldn't count
@ -391,6 +391,9 @@ func usersCreate(t *testing.T, ctx context.Context, db *users) {
"alice",
"alice@example.com",
CreateUserOptions{
FullName: "Alice Jones",
Location: "Earth",
Website: "alice@example.com",
Activated: true,
},
)
@ -427,10 +430,14 @@ func usersCreate(t *testing.T, ctx context.Context, db *users) {
assert.Equal(t, wantErr, err)
})
user, err := db.GetByUsername(ctx, alice.Name)
got, err := db.GetByUsername(ctx, alice.Name)
require.NoError(t, err)
assert.Equal(t, db.NowFunc().Format(time.RFC3339), user.Created.UTC().Format(time.RFC3339))
assert.Equal(t, db.NowFunc().Format(time.RFC3339), user.Updated.UTC().Format(time.RFC3339))
assert.Equal(t, alice.Name, got.Name)
assert.Equal(t, alice.FullName, got.FullName)
assert.Equal(t, alice.Location, got.Location)
assert.Equal(t, alice.Website, got.Website)
assert.Equal(t, db.NowFunc().Format(time.RFC3339), got.Created.UTC().Format(time.RFC3339))
assert.Equal(t, db.NowFunc().Format(time.RFC3339), got.Updated.UTC().Format(time.RFC3339))
}
func usersDeleteCustomAvatar(t *testing.T, ctx context.Context, db *users) {
@ -440,7 +447,7 @@ func usersDeleteCustomAvatar(t *testing.T, ctx context.Context, db *users) {
avatar, err := public.Files.ReadFile("img/avatar_default.png")
require.NoError(t, err)
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "usersDeleteCustomAvatar-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
avatarPath := userutil.CustomAvatarPath(alice.ID)
@ -485,13 +492,13 @@ func usersDeleteByID(t *testing.T, ctx context.Context, db *users) {
assert.Equal(t, wantErr, err)
})
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "usersDeleteByID-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
t.Run("user still has organization membership", func(t *testing.T) {
bob, err := db.Create(ctx, "bob", "bob@exmaple.com", CreateUserOptions{})
require.NoError(t, err)
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)
@ -568,8 +575,6 @@ func usersDeleteByID(t *testing.T, ctx context.Context, db *users) {
require.NoError(t, err)
// Mock user custom avatar
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "usersDeleteByID-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
err = os.MkdirAll(tempPictureAvatarUploadPath, os.ModePerm)
require.NoError(t, err)
tempCustomAvatarPath := userutil.CustomAvatarPath(testUser.ID)
@ -685,7 +690,7 @@ func usersDeleteInactivated(t *testing.T, ctx context.Context, db *users) {
bob, err := db.Create(ctx, "bob", "bob@exmaple.com", CreateUserOptions{})
require.NoError(t, err)
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "usersDeleteInactivated-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
_, err = NewOrganizationsStore(db.DB).Create(ctx, "org1", bob.ID, CreateOrganizationOptions{})
@ -722,7 +727,7 @@ func usersGetByEmail(t *testing.T, ctx context.Context, db *users) {
})
t.Run("ignore organization", func(t *testing.T) {
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "usersGetByEmail-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
org, err := NewOrganizationsStore(db.DB).Create(ctx, "gogs", 1, CreateOrganizationOptions{Email: "gogs@example.com"})
@ -802,7 +807,7 @@ func usersGetByUsername(t *testing.T, ctx context.Context, db *users) {
})
t.Run("wrong user type", func(t *testing.T) {
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "usersGetByUsername-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
org1, err := NewOrganizationsStore(db.DB).Create(ctx, "org1", 1, CreateOrganizationOptions{})
@ -910,7 +915,7 @@ func usersList(t *testing.T, ctx context.Context, db *users) {
require.NoError(t, err)
// Create an organization shouldn't count
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "usersList-tempPictureAvatarUploadPath")
conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
_, err = NewOrganizationsStore(db.DB).Create(ctx, "org1", bob.ID, CreateOrganizationOptions{})