Skip to content

Commit

Permalink
Add some tests (#3030)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerty287 authored Dec 27, 2023
1 parent 5cb0ae0 commit e575ffe
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 2 deletions.
14 changes: 14 additions & 0 deletions cmd/server/swagger_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
27 changes: 27 additions & 0 deletions pipeline/backend/common/script_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion server/plugins/environments/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions server/plugins/environments/parse_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
File renamed without changes.
18 changes: 18 additions & 0 deletions server/plugins/permissions/admins_test.go
Original file line number Diff line number Diff line change
@@ -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"}))
}
20 changes: 20 additions & 0 deletions server/plugins/permissions/orgs_test.go
Original file line number Diff line number Diff line change
@@ -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"}}))
}
2 changes: 1 addition & 1 deletion shared/utils/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions shared/utils/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{""}))
}

0 comments on commit e575ffe

Please sign in to comment.