mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-07-24 18:38:42 +00:00
Followup of #11356 to convert `tests.CreateDeclarativeRepo` to `forgery.CreateRepository` (34 occurrences remaining after this PR - 39 occurrences replaced here). Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12555 Reviewed-by: limiting-factor <limiting-factor@noreply.codeberg.org> Reviewed-by: Gusted <gusted@noreply.codeberg.org>
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
// Copyright 2026 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package forgery
|
|
|
|
import (
|
|
"math/rand/v2"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
org_model "forgejo.org/models/organization"
|
|
user_model "forgejo.org/models/user"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var nameCleaner = regexp.MustCompile(`[^a-zA-Z0-9-]+`) // exclude "_", to prevent multiple consecutive dashes
|
|
|
|
// uniqueSafeName replaces specials chars with _ and appends a random hex suffix
|
|
func uniqueSafeName(testName string) string {
|
|
return strings.Trim(nameCleaner.ReplaceAllLiteralString(testName, "_"), "-_") + "-" + strconv.FormatUint(uint64(rand.Uint32()), 16)
|
|
}
|
|
|
|
type CreateUserOptions struct {
|
|
IsAdmin bool
|
|
}
|
|
|
|
const userPassword = "password"
|
|
|
|
func CreateUser(t testing.TB, opts *CreateUserOptions) *user_model.User {
|
|
t.Helper()
|
|
|
|
if opts == nil {
|
|
opts = &CreateUserOptions{}
|
|
}
|
|
u := &user_model.User{}
|
|
|
|
name := "user-" + uniqueSafeName(t.Name())
|
|
|
|
u.Name = name
|
|
u.Email = name + "@test.forgejo.org"
|
|
u.Passwd = userPassword
|
|
u.IsAdmin = opts.IsAdmin
|
|
|
|
err := user_model.CreateUser(t.Context(), u)
|
|
require.NoError(t, err)
|
|
return u
|
|
}
|
|
|
|
func CreateOrganisation(t testing.TB, owner *user_model.User) *org_model.Organization {
|
|
t.Helper()
|
|
|
|
if owner == nil {
|
|
owner = CreateUser(t, nil) // if specific options are needed, create the owner manually
|
|
}
|
|
o := &org_model.Organization{}
|
|
|
|
name := "org-" + uniqueSafeName(t.Name())
|
|
|
|
o.Name = name
|
|
|
|
err := org_model.CreateOrganization(t.Context(), o, owner)
|
|
require.NoError(t, err)
|
|
return o
|
|
}
|