Skip to content

Commit

Permalink
chore: use table testing
Browse files Browse the repository at this point in the history
  • Loading branch information
1995parham committed Oct 23, 2024
1 parent 0bd1a31 commit 6fbdea0
Showing 1 changed file with 29 additions and 53 deletions.
82 changes: 29 additions & 53 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,68 +186,44 @@ func (suite *APITestSuite) TestValidToken() {
token, err := getSampleToken(suite.key)
require.NoError(err)

body, err := json.Marshal(api.AuthRequest{
Token: "",
Username: "snapp-admin:" + token,
Password: "",
ClientID: "",
})
require.NoError(err)

req := httptest.NewRequest(http.MethodPost, "/v2/auth", bytes.NewReader(body))
req.Header.Add("Content-Type", "application/json")

resp, err := suite.app.Test(req)
require.NoError(err)

defer resp.Body.Close()

require.Equal(http.StatusOK, resp.StatusCode)

data, err := io.ReadAll(resp.Body)
require.NoError(err)

var authResp api.AuthResponse

require.NoError(json.Unmarshal(data, &authResp))

require.Equal("allow", authResp.Result)
require.True(authResp.IsSuperuser)
}

func (suite *APITestSuite) TestValidTokenWithDefaultVendor() {
require := suite.Require()

token, err := getSampleToken(suite.key)
require.NoError(err)
cases := []struct {
username string
}{
{username: "snapp-admin:" + token},
{username: token},
}

body, err := json.Marshal(api.AuthRequest{
Token: "",
Username: token,
Password: "",
ClientID: "",
})
require.NoError(err)
for _, c := range cases {
suite.Run("username "+c.username, func() {
body, err := json.Marshal(api.AuthRequest{
Token: "",
Username: c.username,
Password: "",
ClientID: "",
})
require.NoError(err)

req := httptest.NewRequest(http.MethodPost, "/v2/auth", bytes.NewReader(body))
req.Header.Add("Content-Type", "application/json")
req := httptest.NewRequest(http.MethodPost, "/v2/auth", bytes.NewReader(body))
req.Header.Add("Content-Type", "application/json")

resp, err := suite.app.Test(req)
require.NoError(err)
resp, err := suite.app.Test(req)
require.NoError(err)

defer resp.Body.Close()
defer resp.Body.Close()

require.Equal(http.StatusOK, resp.StatusCode)
require.Equal(http.StatusOK, resp.StatusCode)

data, err := io.ReadAll(resp.Body)
require.NoError(err)
data, err := io.ReadAll(resp.Body)
require.NoError(err)

var authResp api.AuthResponse
var authResp api.AuthResponse

require.NoError(json.Unmarshal(data, &authResp))
require.NoError(json.Unmarshal(data, &authResp))

require.Equal("allow", authResp.Result)
require.True(authResp.IsSuperuser)
require.Equal("allow", authResp.Result)
require.True(authResp.IsSuperuser)
})
}
}

func TestAPITestSuite(t *testing.T) {
Expand Down

0 comments on commit 6fbdea0

Please sign in to comment.