Skip to content

Commit 12d6510

Browse files
committed
Add unit tests for subattester discovery endpoint
Add unit tests for various numbers of subattesters to test endpoint /ratsd/subattesters. Signed-off-by: Ian Chin Wang <[email protected]>
1 parent 9ba476d commit 12d6510

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

api/server_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
mock_deps "github.com/veraison/ratsd/api/mocks"
2020
"github.com/veraison/ratsd/attesters/mocktsm"
2121
"github.com/veraison/ratsd/tokens"
22+
"github.com/veraison/ratsd/attesters/tsm"
2223
"github.com/veraison/services/log"
2324
)
2425

@@ -27,6 +28,54 @@ const (
2728
validNonce = "TUlEQk5IMjhpaW9pc2pQeXh4eHh4eHh4eHh4eHh4eHhNSURCTkgyOGlpb2lzalB5eHh4eHh4eHh4eHh4eHh4eA"
2829
)
2930

31+
func TestRatsdSubattesters_valid_requests(t *testing.T) {
32+
ctrl := gomock.NewController(t)
33+
defer ctrl.Finish()
34+
35+
dm := mock_deps.NewMockIManager(ctrl)
36+
dm.EXPECT().GetPluginList().Return([]string{}).Times(1)
37+
dm.EXPECT().GetPluginList().Return([]string{"mock-tsm"}).Times(1)
38+
dm.EXPECT().GetPluginList().Return([]string{"mock-tsm","tsm-report"}).Times(1)
39+
dm.EXPECT().LookupByName("mock-tsm").Return(mocktsm.GetPlugin(), nil).AnyTimes()
40+
dm.EXPECT().LookupByName("tsm-report").Return(&tsm.TSMPlugin{}, nil).AnyTimes()
41+
logger := log.Named("test")
42+
s := NewServer(logger, dm, "all")
43+
tests := []struct {
44+
name, response string
45+
}{
46+
{
47+
"no attester",
48+
"[]\n",
49+
},
50+
{
51+
"with only mocktsm attester",
52+
"[{\"name\":\"mock-tsm\",\"options\":[{\"data-type\":\"string\",\"name\":\"privilege_level\"}]}]\n",
53+
},
54+
{
55+
"with tsm and mocktsm attester",
56+
"[{\"name\":\"mock-tsm\",\"options\":[{\"data-type\":\"string\",\"name\":\"privilege_level\"}]},{\"name\":\"tsm-report\",\"options\":[{\"data-type\":\"string\",\"name\":\"privilege_level\"}]}]\n",
57+
},
58+
}
59+
60+
for _, tt := range tests {
61+
t.Run(tt.name, func(t *testing.T) {
62+
w := httptest.NewRecorder()
63+
rb := strings.NewReader(tt.response)
64+
r, _ := http.NewRequest(http.MethodGet, "/ratsd/subattesters", rb)
65+
s.RatsdSubattesters(w, r)
66+
67+
expectedCode := http.StatusOK
68+
expectedType := jsonType
69+
expectedBody := tt.response
70+
71+
assert.Equal(t, expectedCode, w.Code)
72+
assert.Equal(t, expectedType, w.Result().Header.Get("Content-Type"))
73+
assert.Equal(t, expectedBody, w.Body.String())
74+
})
75+
}
76+
77+
}
78+
3079
func TestRatsdChares_wrong_content_type(t *testing.T) {
3180
expectedCode := http.StatusBadRequest
3281
expectedType := problems.ProblemMediaType

0 commit comments

Comments
 (0)