From e575ffe72db2d9eac1d79a60f904ee91b02be07c Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Wed, 27 Dec 2023 10:36:49 +0100 Subject: [PATCH] Add some tests (#3030) --- cmd/server/swagger_test.go | 14 ++++++++++ pipeline/backend/common/script_test.go | 27 +++++++++++++++++++ server/plugins/environments/parse.go | 2 +- server/plugins/environments/parse_test.go | 26 ++++++++++++++++++ .../permissions/{admin.go => admins.go} | 0 server/plugins/permissions/admins_test.go | 18 +++++++++++++ server/plugins/permissions/orgs_test.go | 20 ++++++++++++++ shared/utils/slices.go | 2 +- shared/utils/slices_test.go | 10 +++++++ 9 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 cmd/server/swagger_test.go create mode 100644 pipeline/backend/common/script_test.go create mode 100644 server/plugins/environments/parse_test.go rename server/plugins/permissions/{admin.go => admins.go} (100%) create mode 100644 server/plugins/permissions/admins_test.go create mode 100644 server/plugins/permissions/orgs_test.go diff --git a/cmd/server/swagger_test.go b/cmd/server/swagger_test.go new file mode 100644 index 000000000..5d678aed9 --- /dev/null +++ b/cmd/server/swagger_test.go @@ -0,0 +1,14 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.woodpecker-ci.org/woodpecker/v2/cmd/server/docs" +) + +func TestSetupSwaggerStaticConfig(t *testing.T) { + setupSwaggerStaticConfig() + assert.Equal(t, "/api", docs.SwaggerInfo.BasePath) +} diff --git a/pipeline/backend/common/script_test.go b/pipeline/backend/common/script_test.go new file mode 100644 index 000000000..15da65189 --- /dev/null +++ b/pipeline/backend/common/script_test.go @@ -0,0 +1,27 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +const ( + windowsScriptBase64 = "CiRFcnJvckFjdGlvblByZWZlcmVuY2UgPSAnU3RvcCc7CiZjbWQgL2MgIm1rZGlyIGM6XHJvb3QiOwppZiAoJEVudjpDSV9ORVRSQ19NQUNISU5FKSB7CiRuZXRyYz1bc3RyaW5nXTo6Rm9ybWF0KCJ7MH1cX25ldHJjIiwkRW52OkhPTUUpOwoibWFjaGluZSAkRW52OkNJX05FVFJDX01BQ0hJTkUiID4+ICRuZXRyYzsKImxvZ2luICRFbnY6Q0lfTkVUUkNfVVNFUk5BTUUiID4+ICRuZXRyYzsKInBhc3N3b3JkICRFbnY6Q0lfTkVUUkNfUEFTU1dPUkQiID4+ICRuZXRyYzsKfTsKW0Vudmlyb25tZW50XTo6U2V0RW52aXJvbm1lbnRWYXJpYWJsZSgiQ0lfTkVUUkNfUEFTU1dPUkQiLCRudWxsKTsKW0Vudmlyb25tZW50XTo6U2V0RW52aXJvbm1lbnRWYXJpYWJsZSgiQ0lfU0NSSVBUIiwkbnVsbCk7CgpXcml0ZS1PdXRwdXQgKCcrICJlY2hvIGhlbGxvIHdvcmxkIicpOwomIGVjaG8gaGVsbG8gd29ybGQ7IGlmICgkTEFTVEVYSVRDT0RFIC1uZSAwKSB7ZXhpdCAkTEFTVEVYSVRDT0RFfQoK" + posixScriptBase64 = "CmlmIFsgLW4gIiRDSV9ORVRSQ19NQUNISU5FIiBdOyB0aGVuCmNhdCA8PEVPRiA+ICRIT01FLy5uZXRyYwptYWNoaW5lICRDSV9ORVRSQ19NQUNISU5FCmxvZ2luICRDSV9ORVRSQ19VU0VSTkFNRQpwYXNzd29yZCAkQ0lfTkVUUkNfUEFTU1dPUkQKRU9GCmNobW9kIDA2MDAgJEhPTUUvLm5ldHJjCmZpCnVuc2V0IENJX05FVFJDX1VTRVJOQU1FCnVuc2V0IENJX05FVFJDX1BBU1NXT1JECnVuc2V0IENJX1NDUklQVAoKZWNobyArICdlY2hvIGhlbGxvIHdvcmxkJwplY2hvIGhlbGxvIHdvcmxkCg==" +) + +func TestGenerateContainerConf(t *testing.T) { + gotEnv, gotEntry, gotCmd := GenerateContainerConf([]string{"echo hello world"}, "windows") + assert.Equal(t, windowsScriptBase64, gotEnv["CI_SCRIPT"]) + assert.Equal(t, "c:\\root", gotEnv["HOME"]) + assert.Equal(t, "powershell.exe", gotEnv["SHELL"]) + assert.Equal(t, []string{"powershell", "-noprofile", "-noninteractive", "-command"}, gotEntry) + assert.Equal(t, []string{"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($Env:CI_SCRIPT)) | iex"}, gotCmd) + gotEnv, gotEntry, gotCmd = GenerateContainerConf([]string{"echo hello world"}, "linux") + assert.Equal(t, posixScriptBase64, gotEnv["CI_SCRIPT"]) + assert.Equal(t, "/root", gotEnv["HOME"]) + assert.Equal(t, "/bin/sh", gotEnv["SHELL"]) + assert.Equal(t, []string{"/bin/sh", "-c"}, gotEntry) + assert.Equal(t, []string{"echo $CI_SCRIPT | base64 -d | /bin/sh -e"}, gotCmd) +} diff --git a/server/plugins/environments/parse.go b/server/plugins/environments/parse.go index 09935a5a1..b06294c2e 100644 --- a/server/plugins/environments/parse.go +++ b/server/plugins/environments/parse.go @@ -26,7 +26,7 @@ type builtin struct { globals []*model.Environ } -// Parse returns a EnvironService based on a string slice where key and value are separated by a ":" delimiter. +// Parse returns a model.EnvironService based on a string slice where key and value are separated by a ":" delimiter. func Parse(params []string) model.EnvironService { var globals []*model.Environ diff --git a/server/plugins/environments/parse_test.go b/server/plugins/environments/parse_test.go new file mode 100644 index 000000000..869fa7e06 --- /dev/null +++ b/server/plugins/environments/parse_test.go @@ -0,0 +1,26 @@ +package environments + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParse(t *testing.T) { + service := Parse([]string{}) + env, err := service.EnvironList(nil) + assert.NoError(t, err) + assert.Empty(t, env) + + service = Parse([]string{"ENV:value"}) + env, err = service.EnvironList(nil) + assert.NoError(t, err) + assert.Len(t, env, 1) + assert.Equal(t, env[0].Name, "ENV") + assert.Equal(t, env[0].Value, "value") + + service = Parse([]string{"ENV:value", "ENV2:value2"}) + env, err = service.EnvironList(nil) + assert.NoError(t, err) + assert.Len(t, env, 2) +} diff --git a/server/plugins/permissions/admin.go b/server/plugins/permissions/admins.go similarity index 100% rename from server/plugins/permissions/admin.go rename to server/plugins/permissions/admins.go diff --git a/server/plugins/permissions/admins_test.go b/server/plugins/permissions/admins_test.go new file mode 100644 index 000000000..d1e590580 --- /dev/null +++ b/server/plugins/permissions/admins_test.go @@ -0,0 +1,18 @@ +package permissions + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.woodpecker-ci.org/woodpecker/v2/server/model" +) + +func TestAdmins(t *testing.T) { + a := NewAdmins([]string{"woodpecker-ci"}) + assert.True(t, a.IsAdmin(&model.User{Login: "woodpecker-ci"})) + assert.False(t, a.IsAdmin(&model.User{Login: "not-woodpecker-ci"})) + empty := NewAdmins([]string{}) + assert.False(t, empty.IsAdmin(&model.User{Login: "woodpecker-ci"})) + assert.False(t, empty.IsAdmin(&model.User{Login: "not-woodpecker-ci"})) +} diff --git a/server/plugins/permissions/orgs_test.go b/server/plugins/permissions/orgs_test.go new file mode 100644 index 000000000..431a52c96 --- /dev/null +++ b/server/plugins/permissions/orgs_test.go @@ -0,0 +1,20 @@ +package permissions + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.woodpecker-ci.org/woodpecker/v2/server/model" +) + +func TestOrgs(t *testing.T) { + o := NewOrgs([]string{"woodpecker-ci"}) + assert.True(t, o.IsConfigured) + assert.True(t, o.IsMember([]*model.Team{{Login: "woodpecker-ci"}})) + assert.False(t, o.IsMember([]*model.Team{{Login: "not-woodpecker-ci"}})) + empty := NewOrgs([]string{}) + assert.False(t, empty.IsConfigured) + assert.False(t, empty.IsMember([]*model.Team{{Login: "woodpecker-ci"}})) + assert.False(t, empty.IsMember([]*model.Team{{Login: "not-woodpecker-ci"}})) +} diff --git a/shared/utils/slices.go b/shared/utils/slices.go index b2f64eb8b..d0732c814 100644 --- a/shared/utils/slices.go +++ b/shared/utils/slices.go @@ -58,7 +58,7 @@ func sliceToCountMap[E comparable](list []E) map[E]int { return m } -// sliceToMap is a helper function to convert a string slice to a map. +// SliceToBoolMap is a helper function to convert a string slice to a map. func SliceToBoolMap(s []string) map[string]bool { v := map[string]bool{} for _, ss := range s { diff --git a/shared/utils/slices_test.go b/shared/utils/slices_test.go index 291b34964..3a499c4ab 100644 --- a/shared/utils/slices_test.go +++ b/shared/utils/slices_test.go @@ -58,3 +58,13 @@ func TestEqualSliceValues(t *testing.T) { assert.True(t, EqualSliceValues([]bool{true, false, false}, []bool{false, false, true})) assert.False(t, EqualSliceValues([]bool{true, false, false}, []bool{true, false, true})) } + +func TestSliceToBoolMap(t *testing.T) { + assert.Equal(t, map[string]bool{ + "a": true, + "b": true, + "c": true, + }, SliceToBoolMap([]string{"a", "b", "c"})) + assert.Equal(t, map[string]bool{}, SliceToBoolMap([]string{})) + assert.Equal(t, map[string]bool{}, SliceToBoolMap([]string{""})) +}