Skip to content

Commit

Permalink
use subtests and add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
umbynos committed Sep 6, 2023
1 parent 6e6bd99 commit b7d18c2
Showing 1 changed file with 53 additions and 18 deletions.
71 changes: 53 additions & 18 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -48,34 +49,68 @@ func TestValidSignatureKey(t *testing.T) {
require.NotNil(t, key)
}

func TestInstallToolDifferentContentType(t *testing.T) {
func TestInstallToolV2(t *testing.T) {
r := gin.New()
goa := v2.Server(config.GetDataDir().String())
r.Any("/v2/*path", gin.WrapH(goa))
ts := httptest.NewServer(r)

URL := "http://downloads.arduino.cc/tools/bossac-1.7.0-arduino3-linux64.tar.gz"
Checksum := "SHA-256:1ae54999c1f97234a5c603eb99ad39313b11746a4ca517269a9285afa05f9100"
request := tools.ToolPayload{
type test struct {
request tools.ToolPayload
responseCode int
responseBody string
}

BossacURL := "http://downloads.arduino.cc/tools/bossac-1.7.0-arduino3-linux64.tar.gz"
BossacChecksum := "SHA-256:1ae54999c1f97234a5c603eb99ad39313b11746a4ca517269a9285afa05f9100"
BossacSignature := "382898a97b5a86edd74208f10107d2fecbf7059ffe9cc856e045266fb4db4e98802728a0859cfdcda1c0b9075ec01e42dbea1f430b813530d5a6ae1766dfbba64c3e689b59758062dc2ab2e32b2a3491dc2b9a80b9cda4ae514fbe0ec5af210111b6896976053ab76bac55bcecfcececa68adfa3299e3cde6b7f117b3552a7d80ca419374bb497e3c3f12b640cf5b20875416b45e662fc6150b99b178f8e41d6982b4c0a255925ea39773683f9aa9201dc5768b6fc857c87ff602b6a93452a541b8ec10ca07f166e61a9e9d91f0a6090bd2038ed4427af6251039fb9fe8eb62ec30d7b0f3df38bc9de7204dec478fb86f8eb3f71543710790ee169dce039d3e0"
bossacInstallURLOK := tools.ToolPayload{
Name: "bossac",
Version: "1.7.0-arduino3",
Packager: "arduino",
URL: &BossacURL,
Checksum: &BossacChecksum,
Signature: &BossacSignature,
}

WrongSignature := "wr0ngs1gn4tur3"
bossacInstallWrongSig := tools.ToolPayload{
Name: "bossac",
Version: "1.7.0-arduino3",
Packager: "arduino",
URL: &BossacURL,
Checksum: &BossacChecksum,
Signature: &WrongSignature,
}

bossacInstallNoURL := tools.ToolPayload{
Name: "bossac",
Version: "1.7.0-arduino3",
Packager: "arduino",
URL: &URL,
Checksum: &Checksum,
}

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

for _, test := range tests {
t.Run(fmt.Sprintf("Installing %s", test.request.Name), func(t *testing.T) {
payload, err := json.Marshal(test.request)
require.NoError(t, err)

// for some reason the fronted sends requests with "text/plain" content type.
// Even if the request body contains a json object.
// With this test we verify is parsed correctly.
for _, encoding := range []string{"encoding/json", "text/plain"} {
resp, err := http.Post(ts.URL+"/v2/pkgs/tools/installed", encoding, bytes.NewBuffer(payload))
require.NoError(t, err)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), "ok")
require.Equal(t, http.StatusOK, resp.StatusCode)
// for some reason the fronted sends requests with "text/plain" content type.
// Even if the request body contains a json object.
// With this test we verify is parsed correctly.
for _, encoding := range []string{"encoding/json", "text/plain"} {
resp, err := http.Post(ts.URL+"/v2/pkgs/tools/installed", encoding, bytes.NewBuffer(payload))
require.NoError(t, err)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), test.responseBody)
require.Equal(t, test.responseCode, resp.StatusCode)
}
})
}
}

0 comments on commit b7d18c2

Please # to comment.