Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error test #175

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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