Skip to content

Commit

Permalink
chore: build default configuration with default values, add functiona…
Browse files Browse the repository at this point in the history
…l tests of examples
  • Loading branch information
pandatix committed Dec 8, 2024
1 parent 9191ce5 commit 8f7837e
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 9 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jobs:
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-go-

- name: Run unit tests
run: |
go test ./ -run=^Test_U -coverprofile=unit.out -json | tee -a gotest.json
- name: Wait for CTFd server
run: |
max_attempts=60
Expand All @@ -51,13 +55,19 @@ jobs:
exit 1
fi
- name: Setup CTFd
- name: Run functional tests
run: |
go test ./ -run=^Test_F -coverprofile=functional.out -json | tee -a gotest.json
env:
URL: http://localhost:8000

- name: Run integration test
run: |
go build -cover -o ctfd-setup cmd/ctfd-setup/main.go
mkdir coverdir
GOCOVERDIR=coverdir ./ctfd-setup
go tool covdata textfmt -i=coverdir -o cov.out
sed -i '/^\//d' cov.out
go tool covdata textfmt -i=coverdir -o integration.out
sed -i '/^\//d' integration.out
env:
URL: http://localhost:8000
APPEARANCE_NAME: 'CTFer.io'
Expand All @@ -66,6 +76,11 @@ jobs:
ADMIN_EMAIL: 'ctfer-io@protonmail.com'
ADMIN_PASSWORD: 'ctfer'

- name: Merge coverage data
run: |
go install go.shabbyrobe.org/gocovmerge/cmd/gocovmerge@fa4f82cfbf4d57c646c1ed0f35002bf1b89fbf7a
gocovmerge unit.out functional.out integration.out > cov.out
- name: Upload coverage to Coveralls
uses: shogo82148/actions-goveralls@785c9d68212c91196d3994652647f8721918ba11 # v1.9.0
with:
Expand Down
40 changes: 35 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type (
SmallIcon *File `yaml:"small_icon,omitempty" json:"small_icon,omitempty"`

// The frontend theme name.
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty" jsonschema:"default=core-beta"` // do not restrict to core-beta or core (deprecated) to avoid limiting to official themes

// The frontend theme color.
Color string `yaml:"color,omitempty" json:"color,omitempty"`
Expand Down Expand Up @@ -117,16 +117,16 @@ type (
// Settings for ressources visibility.
Settings struct {
// The visibility for the challenges. Please refer to CTFd documentation (https://docs.ctfd.io/docs/settings/visibility-settings/).
ChallengeVisibility string `yaml:"challenge_visibility,omitempty" json:"challenge_visibility,omitempty"`
ChallengeVisibility string `yaml:"challenge_visibility,omitempty" json:"challenge_visibility,omitempty" jsonschema:"enum=public,enum=private,enum=admins,default=private"`

// The visibility for the accounts. Please refer to CTFd documentation (https://docs.ctfd.io/docs/settings/visibility-settings/).
AccountVisibility string `yaml:"account_visibility,omitempty" json:"account_visibility,omitempty"`
AccountVisibility string `yaml:"account_visibility,omitempty" json:"account_visibility,omitempty" jsonschema:"enum=public,enum=private,enum=admins,default=public"`

// The visibility for the scoreboard. Please refer to CTFd documentation (https://docs.ctfd.io/docs/settings/visibility-settings/).
ScoreVisibility string `yaml:"score_visibility,omitempty" json:"score_visibility,omitempty"`
ScoreVisibility string `yaml:"score_visibility,omitempty" json:"score_visibility,omitempty" jsonschema:"enum=public,enum=private,enum=admins,default=public"`

// The visibility for the registration. Please refer to CTFd documentation (https://docs.ctfd.io/docs/settings/visibility-settings/).
RegistrationVisibility string `yaml:"registration_visibility,omitempty" json:"registration_visibility,omitempty"`
RegistrationVisibility string `yaml:"registration_visibility,omitempty" json:"registration_visibility,omitempty" jsonschema:"enum=public,enum=private,enum=admins,default=public"`

// Whether the CTFd is paused or not.
Paused *bool `yaml:"paused,omitempty" json:"paused,omitempty"`
Expand Down Expand Up @@ -239,6 +239,36 @@ type (
}
)

func NewConfig() *Config {
return &Config{
Theme: &Theme{
Name: "core-beta",
Logo: &File{},
SmallIcon: &File{},
Header: &File{},
Footer: &File{},
Settings: &File{},
},
Accounts: &Accounts{},
Pages: &Pages{
RobotsTxt: &File{},
},
MajorLeagueCyber: &MajorLeagueCyber{},
Settings: &Settings{
ChallengeVisibility: "private", // default value
AccountVisibility: "public", // default value
ScoreVisibility: "public", // default value
RegistrationVisibility: "public", // default value
},
Security: &Security{},
Email: &Email{},
Time: &Time{},
Social: &Social{},
Legal: &Legal{},
Mode: "users", // default value
}
}

// Schema returns the JSON schema for the configuration file.
func (conf Config) Schema() ([]byte, error) {
reflector := jsonschema.Reflector{}
Expand Down
81 changes: 80 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,96 @@
package ctfdsetup_test

import (
"context"
"os"
"path/filepath"
"testing"

ctfdsetup "github.com/ctfer-io/ctfd-setup"
"github.com/ctfer-io/go-ctfd/api"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)

const (
dir = "examples"
)

func Test_U_ConfigSchema(t *testing.T) {
t.Parallel()
assert := assert.New(t)

cfg := &ctfdsetup.Config{}
cfg := ctfdsetup.NewConfig()

schema, err := cfg.Schema()
assert.NoError(err)
assert.NotEmpty(schema)
}

func Test_F_Examples(t *testing.T) {
url, ok := os.LookupEnv("URL")
if !ok {
t.Fatal("environment variable URL is not defined")
}

files, err := os.ReadDir(dir)
if !assert.NoError(t, err) {
return
}

for _, f := range files {
if f.IsDir() {
continue
}

t.Run(f.Name(), func(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()

c, err := os.ReadFile(filepath.Join(dir, f.Name()))
if !assert.NoError(err) {
return
}

// Extract config
cfg := ctfdsetup.NewConfig()
err = yaml.Unmarshal(c, cfg)
assert.NoError(err)

err = cfg.Validate()
if !assert.NoError(err) {
return
}

// Login then reset (required to run multiple test cases)
defer func() {
nonce, session, err := api.GetNonceAndSession(url, api.WithContext(ctx))
assert.NoError(err)
client := api.NewClient(url, nonce, session, "")

err = client.Login(&api.LoginParams{
Name: cfg.Admin.Name,
Password: cfg.Admin.Password.Content,
}, api.WithContext(ctx))
assert.NoError(err)

err = client.Reset(&api.ResetParams{
Accounts: ptr("true"),
Submissions: ptr("true"),
Challenges: ptr("true"),
Pages: ptr("true"),
Notifications: ptr("true"),
})
assert.NoError(err)
}()

// Setup CTFd
err = ctfdsetup.Setup(ctx, url, "", cfg)
assert.NoError(err)
})
}
}

func ptr[T any](t T) *T {
return &t
}

0 comments on commit 8f7837e

Please sign in to comment.