Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 4e3150b

Browse files
authored
Merge pull request #29 from vulncheck-oss/28-backups-list
Issue #28 - List Backups Endpoint
2 parents 6e7daa5 + 6842dd7 commit 4e3150b

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

backups.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package sdk
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
type BackupsMeta struct {
9+
Name string `json:"name"`
10+
Description string `json:"description"`
11+
Href string `json:"href"`
12+
}
13+
14+
type BackupsResponse struct {
15+
Benchmark float64 `json:"_benchmark"`
16+
Data []BackupsMeta `json:"data"`
17+
}
18+
19+
// GetBackups https://docs.vulncheck.com/api/backups
20+
func (c *Client) GetBackups() (responseJSON *BackupsResponse, err error) {
21+
22+
resp, err := c.Request("GET", "/v3/backup")
23+
if err != nil {
24+
return nil, err
25+
}
26+
defer resp.Body.Close()
27+
_ = json.NewDecoder(resp.Body).Decode(&responseJSON)
28+
return responseJSON, nil
29+
}
30+
31+
// Strings representation of the response
32+
func (r BackupsResponse) String() string {
33+
return fmt.Sprintf("Benchmark: %v\nData: %v\n", r.Benchmark, r.Data)
34+
}
35+
36+
// GetData - Returns the data from the response
37+
func (r BackupsResponse) GetData() []BackupsMeta {
38+
return r.Data
39+
}

backups_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package sdk
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"net/http/httptest"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestGetBackups(t *testing.T) {
15+
req := httptest.NewRequest("GET", "/backup", nil)
16+
w := httptest.NewRecorder()
17+
18+
mockJson := `{"_benchmark":0.02508,"data":[{"name":"a10","description":"A10 Networks Security Advisories","href":"https://api.vulncheck.com/v3/index/a10"}]}`
19+
20+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21+
fmt.Fprintln(w, mockJson)
22+
})
23+
24+
handler.ServeHTTP(w, req)
25+
26+
resp := w.Result()
27+
28+
assert.Equal(t, 200, resp.StatusCode)
29+
30+
body, _ := io.ReadAll(resp.Body)
31+
32+
var backupResp BackupsResponse
33+
err := json.Unmarshal(body, &backupResp)
34+
if err != nil {
35+
t.Error("error unmarshalling response")
36+
}
37+
38+
t.Run("indexes response is parsed", func(t *testing.T) {
39+
assert.Equal(t, "a10", backupResp.Data[0].Name)
40+
})
41+
}
42+
43+
// LIVE TESTS - these tests require a valid token to run
44+
// func TestGetBackupsLive(t *testing.T) {
45+
// t.Run("GetBackups", func(t *testing.T) {
46+
// client := Connect("", "")
47+
48+
// data, err := client.GetBackups()
49+
// if err != nil {
50+
// t.Errorf("Error: %v", err)
51+
// }
52+
53+
// if data == nil {
54+
// t.Errorf("Expected a string, got %v", data)
55+
// }
56+
// })
57+
// }

0 commit comments

Comments
 (0)