Skip to content

Commit

Permalink
Add error test
Browse files Browse the repository at this point in the history
  • Loading branch information
winebarrel committed Jun 11, 2024
1 parent 1a45a82 commit 930dfcd
Showing 1 changed file with 206 additions and 0 deletions.
206 changes: 206 additions & 0 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,40 @@ func Test_ListUsers_OK(t *testing.T) {
}, res)
}

func Test_ListUsers_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/users", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.ListUsers(context.Background(), &redash.ListUsersInput{
Page: 1,
PageSize: 25,
})
assert.ErrorContains(err, "GET api/users failed: HTTP status code not OK: 503\nerror")
}

func Test_ListUsers_IOErr(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/users", func(req *http.Request) (*http.Response, error) {
return testIOErrResp, nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.ListUsers(context.Background(), &redash.ListUsersInput{
Page: 1,
PageSize: 25,
})
assert.ErrorContains(err, "Read response body failed: IO error")
}

func Test_GetUser_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -161,6 +195,34 @@ func Test_GetUser_OK(t *testing.T) {
}, res)
}

func Test_GetUser_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/users/1", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.GetUser(context.Background(), 1)
assert.ErrorContains(err, "GET api/users/1 failed: HTTP status code not OK: 503\nerror")
}

func Test_GetUser_IOErr(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/users/1", func(req *http.Request) (*http.Response, error) {
return testIOErrResp, nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.GetUser(context.Background(), 1)
assert.ErrorContains(err, "Read response body failed: IO error")
}

func Test_CreateUser_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -230,6 +292,42 @@ func Test_CreateUser_OK(t *testing.T) {
}, res)
}

func Test_CreateUser_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/users", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.CreateUser(context.Background(), &redash.CreateUsersInput{
AuthType: "password",
Email: "[email protected]",
Name: "admin",
})
assert.ErrorContains(err, "POST api/users failed: HTTP status code not OK: 503\nerror")
}

func Test_CreateUser_IOErr(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/users", func(req *http.Request) (*http.Response, error) {
return testIOErrResp, nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.CreateUser(context.Background(), &redash.CreateUsersInput{
AuthType: "password",
Email: "[email protected]",
Name: "admin",
})
assert.ErrorContains(err, "Read response body failed: IO error")
}

func Test_UpdateUser_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -298,6 +396,44 @@ func Test_UpdateUser_OK(t *testing.T) {
}, res)
}

func Test_UpdateUser_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/users/1", func(req *http.Request) (*http.Response, error) {
body, _ := io.ReadAll(req.Body)
assert.Equal(`{"email":"[email protected]","name":"admin2"}`, string(body))
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.UpdateUser(context.Background(), 1, &redash.UpdateUserInput{
Email: "[email protected]",
Name: "admin2",
})
assert.ErrorContains(err, "POST api/users/1 failed: HTTP status code not OK: 503\nerror")
}

func Test_UpdateUser_IOErr(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/users/1", func(req *http.Request) (*http.Response, error) {
body, _ := io.ReadAll(req.Body)
assert.Equal(`{"email":"[email protected]","name":"admin2"}`, string(body))
return testIOErrResp, nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.UpdateUser(context.Background(), 1, &redash.UpdateUserInput{
Email: "[email protected]",
Name: "admin2",
})
assert.ErrorContains(err, "Read response body failed: IO error")
}

func Test_DeleteUser_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand All @@ -322,6 +458,20 @@ func Test_DeleteUser_OK(t *testing.T) {
assert.NoError(err)
}

func Test_DeleteUser_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodDelete, "https://redash.example.com/api/users/1", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
err := client.DeleteUser(context.Background(), 1)
assert.ErrorContains(err, "DELETE api/users/1 failed: HTTP status code not OK: 503\nerror")
}

func Test_DisableUser_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -382,6 +532,34 @@ func Test_DisableUser_OK(t *testing.T) {
}, res)
}

func Test_DisableUser_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/users/1/disable", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.DisableUser(context.Background(), 1)
assert.ErrorContains(err, "POST api/users/1/disable failed: HTTP status code not OK: 503\nerror")
}

func Test_DisableUser_IOErr(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodPost, "https://redash.example.com/api/users/1/disable", func(req *http.Request) (*http.Response, error) {
return testIOErrResp, nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.DisableUser(context.Background(), 1)
assert.ErrorContains(err, "Read response body failed: IO error")
}

func Test_EnableUser_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
Expand Down Expand Up @@ -442,6 +620,34 @@ func Test_EnableUser_OK(t *testing.T) {
}, res)
}

func Test_EnableUser_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodDelete, "https://redash.example.com/api/users/1/disable", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.EnableUser(context.Background(), 1)
assert.ErrorContains(err, "DELETE api/users/1/disable failed: HTTP status code not OK: 503\nerror")
}

func Test_EnableUser_IOErr(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodDelete, "https://redash.example.com/api/users/1/disable", func(req *http.Request) (*http.Response, error) {
return testIOErrResp, nil
})

client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.EnableUser(context.Background(), 1)
assert.ErrorContains(err, "Read response body failed: IO error")
}

func Test_User_Acc(t *testing.T) {
if !testAcc {
t.Skip()
Expand Down

0 comments on commit 930dfcd

Please sign in to comment.