Skip to content

Commit b123ca4

Browse files
committed
Add backend unit tests
1 parent 72b071d commit b123ca4

File tree

17 files changed

+399
-283
lines changed

17 files changed

+399
-283
lines changed

backend/Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "2"
1+
version: "3"
22

33
tasks:
44
default:

backend/internal/acme/acmesh.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ func getCommonArgs() []string {
136136
}
137137

138138
// This is split out into it's own function so it's testable
139-
func buildCertRequestArgs(domains []string, method, outputFullchainFile, outputKeyFile string, dnsProvider *dnsprovider.Model, ca *certificateauthority.Model, force bool) ([]string, error) {
139+
func buildCertRequestArgs(
140+
domains []string,
141+
method,
142+
outputFullchainFile,
143+
outputKeyFile string,
144+
dnsProvider *dnsprovider.Model,
145+
ca *certificateauthority.Model,
146+
force bool,
147+
) ([]string, error) {
140148
// The argument order matters.
141149
// see https://github.com/acmesh-official/acme.sh/wiki/How-to-issue-a-cert#3-multiple-domains-san-mode--hybrid-mode
142150
// for multiple domains and note that the method of validation is required just after the ___domain arg, each time.

backend/internal/acme/acmesh_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,33 @@ func TestBuildCertRequestArgs(t *testing.T) {
193193
})
194194
}
195195
}
196+
197+
func TestGetAcmeShFilePath(t *testing.T) {
198+
t.Run("basic test", func(t *testing.T) {
199+
path, err := getAcmeShFilePath()
200+
assert.Equal(t, "/bin/acme.sh", path)
201+
assert.Equal(t, nil, err)
202+
})
203+
}
204+
205+
func TestGetCommonEnvVars(t *testing.T) {
206+
t.Run("basic test", func(t *testing.T) {
207+
expected := []string{
208+
"ACMESH_CONFIG_HOME=/data/.acme.sh/config",
209+
"ACMESH_HOME=/data/.acme.sh",
210+
"CERT_HOME=/data/.acme.sh/certs",
211+
"LE_CONFIG_HOME=/data/.acme.sh/config",
212+
"LE_WORKING_DIR=/data/.acme.sh",
213+
}
214+
vals := getCommonEnvVars()
215+
assert.Equal(t, expected, vals)
216+
})
217+
}
218+
219+
func TestGetAcmeShVersion(t *testing.T) {
220+
t.Run("basic test", func(t *testing.T) {
221+
resp := GetAcmeShVersion()
222+
assert.Greater(t, len(resp), 1)
223+
assert.Equal(t, "v", resp[:1])
224+
})
225+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package context
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestGetString(t *testing.T) {
10+
t.Run("basic test", func(t *testing.T) {
11+
assert.Equal(t, "context value: Body", BodyCtxKey.String())
12+
})
13+
}

backend/internal/api/filters/helpers.go

Lines changed: 0 additions & 208 deletions
This file was deleted.

backend/internal/api/handler/certificates.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func GetCertificates() func(http.ResponseWriter, *http.Request) {
2626
return
2727
}
2828

29-
certificates, err := certificate.List(pageInfo, middleware.GetFiltersFromContext(r), getExpandFromContext(r))
29+
certificates, err := certificate.List(pageInfo, middleware.GetFiltersFromContext(r), middleware.GetExpandFromContext(r))
3030
if err != nil {
3131
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
3232
} else {
@@ -41,7 +41,7 @@ func GetCertificate() func(http.ResponseWriter, *http.Request) {
4141
return func(w http.ResponseWriter, r *http.Request) {
4242
if item := getCertificateFromRequest(w, r); item != nil {
4343
// nolint: errcheck,gosec
44-
item.Expand(getExpandFromContext(r))
44+
item.Expand(middleware.GetExpandFromContext(r))
4545
h.ResultResponseJSON(w, r, http.StatusOK, item)
4646
}
4747
}

backend/internal/api/handler/helpers.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"net/http"
55
"strconv"
66

7-
"npm/internal/api/context"
8-
"npm/internal/api/middleware"
97
"npm/internal/model"
108

119
"github.com/go-chi/chi/v5"
@@ -15,15 +13,15 @@ import (
1513
const defaultLimit = 10
1614

1715
func getPageInfoFromRequest(r *http.Request) (model.PageInfo, error) {
18-
var pageInfo model.PageInfo
16+
pageInfo := model.PageInfo{}
1917
var err error
2018

2119
pageInfo.Offset, pageInfo.Limit, err = getPagination(r)
2220
if err != nil {
2321
return pageInfo, err
2422
}
2523

26-
pageInfo.Sort = middleware.GetSortFromContext(r)
24+
// pageInfo.Sort = middleware.GetSortFromContext(r)
2725

2826
return pageInfo, nil
2927
}
@@ -93,12 +91,3 @@ func getPagination(r *http.Request) (int, int, error) {
9391

9492
return offset, limit, nil
9593
}
96-
97-
// getExpandFromContext returns the Expansion setting
98-
func getExpandFromContext(r *http.Request) []string {
99-
expand, ok := r.Context().Value(context.ExpansionCtxKey).([]string)
100-
if !ok {
101-
return nil
102-
}
103-
return expand
104-
}

0 commit comments

Comments
 (0)