Skip to content

Commit a839105

Browse files
committed
use subtests and add test cases
1 parent 200777e commit a839105

File tree

1 file changed

+53
-18
lines changed

1 file changed

+53
-18
lines changed

main_test.go

+53-18
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"crypto/x509"
2121
"encoding/json"
2222
"encoding/pem"
23+
"fmt"
2324
"io"
2425
"net/http"
2526
"net/http/httptest"
@@ -85,34 +86,68 @@ func TestUploadHandlerAgainstEvilFileNames(t *testing.T) {
8586
}
8687
}
8788

88-
func TestInstallToolDifferentContentType(t *testing.T) {
89+
func TestInstallToolV2(t *testing.T) {
8990
r := gin.New()
9091
goa := v2.Server(config.GetDataDir().String())
9192
r.Any("/v2/*path", gin.WrapH(goa))
9293
ts := httptest.NewServer(r)
9394

94-
URL := "http://downloads.arduino.cc/tools/bossac-1.7.0-arduino3-linux64.tar.gz"
95-
Checksum := "SHA-256:1ae54999c1f97234a5c603eb99ad39313b11746a4ca517269a9285afa05f9100"
96-
request := tools.ToolPayload{
95+
type test struct {
96+
request tools.ToolPayload
97+
responseCode int
98+
responseBody string
99+
}
100+
101+
BossacURL := "http://downloads.arduino.cc/tools/bossac-1.7.0-arduino3-linux64.tar.gz"
102+
BossacChecksum := "SHA-256:1ae54999c1f97234a5c603eb99ad39313b11746a4ca517269a9285afa05f9100"
103+
BossacSignature := "382898a97b5a86edd74208f10107d2fecbf7059ffe9cc856e045266fb4db4e98802728a0859cfdcda1c0b9075ec01e42dbea1f430b813530d5a6ae1766dfbba64c3e689b59758062dc2ab2e32b2a3491dc2b9a80b9cda4ae514fbe0ec5af210111b6896976053ab76bac55bcecfcececa68adfa3299e3cde6b7f117b3552a7d80ca419374bb497e3c3f12b640cf5b20875416b45e662fc6150b99b178f8e41d6982b4c0a255925ea39773683f9aa9201dc5768b6fc857c87ff602b6a93452a541b8ec10ca07f166e61a9e9d91f0a6090bd2038ed4427af6251039fb9fe8eb62ec30d7b0f3df38bc9de7204dec478fb86f8eb3f71543710790ee169dce039d3e0"
104+
bossacInstallURLOK := tools.ToolPayload{
105+
Name: "bossac",
106+
Version: "1.7.0-arduino3",
107+
Packager: "arduino",
108+
URL: &BossacURL,
109+
Checksum: &BossacChecksum,
110+
Signature: &BossacSignature,
111+
}
112+
113+
WrongSignature := "wr0ngs1gn4tur3"
114+
bossacInstallWrongSig := tools.ToolPayload{
115+
Name: "bossac",
116+
Version: "1.7.0-arduino3",
117+
Packager: "arduino",
118+
URL: &BossacURL,
119+
Checksum: &BossacChecksum,
120+
Signature: &WrongSignature,
121+
}
122+
123+
bossacInstallNoURL := tools.ToolPayload{
97124
Name: "bossac",
98125
Version: "1.7.0-arduino3",
99126
Packager: "arduino",
100-
URL: &URL,
101-
Checksum: &Checksum,
102127
}
103128

104-
payload, err := json.Marshal(request)
105-
require.NoError(t, err)
129+
tests := []test{
130+
{bossacInstallURLOK, http.StatusOK, "ok"},
131+
{bossacInstallWrongSig, http.StatusInternalServerError, "verification error"},
132+
{bossacInstallNoURL, http.StatusBadRequest, "tool not found"}, //because the index is not added
133+
}
106134

107-
// for some reason the fronted sends requests with "text/plain" content type.
108-
// Even if the request body contains a json object.
109-
// With this test we verify is parsed correctly.
110-
for _, encoding := range []string{"encoding/json", "text/plain"} {
111-
resp, err := http.Post(ts.URL+"/v2/pkgs/tools/installed", encoding, bytes.NewBuffer(payload))
112-
require.NoError(t, err)
113-
body, err := io.ReadAll(resp.Body)
114-
require.NoError(t, err)
115-
require.Contains(t, string(body), "ok")
116-
require.Equal(t, http.StatusOK, resp.StatusCode)
135+
for _, test := range tests {
136+
t.Run(fmt.Sprintf("Installing %s", test.request.Name), func(t *testing.T) {
137+
payload, err := json.Marshal(test.request)
138+
require.NoError(t, err)
139+
140+
// for some reason the fronted sends requests with "text/plain" content type.
141+
// Even if the request body contains a json object.
142+
// With this test we verify is parsed correctly.
143+
for _, encoding := range []string{"encoding/json", "text/plain"} {
144+
resp, err := http.Post(ts.URL+"/v2/pkgs/tools/installed", encoding, bytes.NewBuffer(payload))
145+
require.NoError(t, err)
146+
body, err := io.ReadAll(resp.Body)
147+
require.NoError(t, err)
148+
require.Contains(t, string(body), test.responseBody)
149+
require.Equal(t, test.responseCode, resp.StatusCode)
150+
}
151+
})
117152
}
118153
}

0 commit comments

Comments
 (0)