Skip to content

Commit b7d18c2

Browse files
committed
use subtests and add test cases
1 parent 6e6bd99 commit b7d18c2

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"
@@ -48,34 +49,68 @@ func TestValidSignatureKey(t *testing.T) {
4849
require.NotNil(t, key)
4950
}
5051

51-
func TestInstallToolDifferentContentType(t *testing.T) {
52+
func TestInstallToolV2(t *testing.T) {
5253
r := gin.New()
5354
goa := v2.Server(config.GetDataDir().String())
5455
r.Any("/v2/*path", gin.WrapH(goa))
5556
ts := httptest.NewServer(r)
5657

57-
URL := "http://downloads.arduino.cc/tools/bossac-1.7.0-arduino3-linux64.tar.gz"
58-
Checksum := "SHA-256:1ae54999c1f97234a5c603eb99ad39313b11746a4ca517269a9285afa05f9100"
59-
request := tools.ToolPayload{
58+
type test struct {
59+
request tools.ToolPayload
60+
responseCode int
61+
responseBody string
62+
}
63+
64+
BossacURL := "http://downloads.arduino.cc/tools/bossac-1.7.0-arduino3-linux64.tar.gz"
65+
BossacChecksum := "SHA-256:1ae54999c1f97234a5c603eb99ad39313b11746a4ca517269a9285afa05f9100"
66+
BossacSignature := "382898a97b5a86edd74208f10107d2fecbf7059ffe9cc856e045266fb4db4e98802728a0859cfdcda1c0b9075ec01e42dbea1f430b813530d5a6ae1766dfbba64c3e689b59758062dc2ab2e32b2a3491dc2b9a80b9cda4ae514fbe0ec5af210111b6896976053ab76bac55bcecfcececa68adfa3299e3cde6b7f117b3552a7d80ca419374bb497e3c3f12b640cf5b20875416b45e662fc6150b99b178f8e41d6982b4c0a255925ea39773683f9aa9201dc5768b6fc857c87ff602b6a93452a541b8ec10ca07f166e61a9e9d91f0a6090bd2038ed4427af6251039fb9fe8eb62ec30d7b0f3df38bc9de7204dec478fb86f8eb3f71543710790ee169dce039d3e0"
67+
bossacInstallURLOK := tools.ToolPayload{
68+
Name: "bossac",
69+
Version: "1.7.0-arduino3",
70+
Packager: "arduino",
71+
URL: &BossacURL,
72+
Checksum: &BossacChecksum,
73+
Signature: &BossacSignature,
74+
}
75+
76+
WrongSignature := "wr0ngs1gn4tur3"
77+
bossacInstallWrongSig := tools.ToolPayload{
78+
Name: "bossac",
79+
Version: "1.7.0-arduino3",
80+
Packager: "arduino",
81+
URL: &BossacURL,
82+
Checksum: &BossacChecksum,
83+
Signature: &WrongSignature,
84+
}
85+
86+
bossacInstallNoURL := tools.ToolPayload{
6087
Name: "bossac",
6188
Version: "1.7.0-arduino3",
6289
Packager: "arduino",
63-
URL: &URL,
64-
Checksum: &Checksum,
6590
}
6691

67-
payload, err := json.Marshal(request)
68-
require.NoError(t, err)
92+
tests := []test{
93+
{bossacInstallURLOK, http.StatusOK, "ok"},
94+
{bossacInstallWrongSig, http.StatusInternalServerError, "verification error"},
95+
{bossacInstallNoURL, http.StatusBadRequest, "tool not found"}, //because the index is not added
96+
}
97+
98+
for _, test := range tests {
99+
t.Run(fmt.Sprintf("Installing %s", test.request.Name), func(t *testing.T) {
100+
payload, err := json.Marshal(test.request)
101+
require.NoError(t, err)
69102

70-
// for some reason the fronted sends requests with "text/plain" content type.
71-
// Even if the request body contains a json object.
72-
// With this test we verify is parsed correctly.
73-
for _, encoding := range []string{"encoding/json", "text/plain"} {
74-
resp, err := http.Post(ts.URL+"/v2/pkgs/tools/installed", encoding, bytes.NewBuffer(payload))
75-
require.NoError(t, err)
76-
body, err := io.ReadAll(resp.Body)
77-
require.NoError(t, err)
78-
require.Contains(t, string(body), "ok")
79-
require.Equal(t, http.StatusOK, resp.StatusCode)
103+
// for some reason the fronted sends requests with "text/plain" content type.
104+
// Even if the request body contains a json object.
105+
// With this test we verify is parsed correctly.
106+
for _, encoding := range []string{"encoding/json", "text/plain"} {
107+
resp, err := http.Post(ts.URL+"/v2/pkgs/tools/installed", encoding, bytes.NewBuffer(payload))
108+
require.NoError(t, err)
109+
body, err := io.ReadAll(resp.Body)
110+
require.NoError(t, err)
111+
require.Contains(t, string(body), test.responseBody)
112+
require.Equal(t, test.responseCode, resp.StatusCode)
113+
}
114+
})
80115
}
81116
}

0 commit comments

Comments
 (0)