Skip to content

Commit 4dcd1fd

Browse files
committed
Stub out client tests
1 parent 4d8f131 commit 4dcd1fd

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

codersdk/client.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ func (c *Client) SetSessionToken(token string) error {
4747
return nil
4848
}
4949

50+
// ClearSessionToken clears tokens and authentication context in the current cline.t
51+
func (c *Client) ClearSessionToken() error {
52+
// If there is no cookie jar defined, just leave it be
53+
if c.httpClient.Jar == nil {
54+
return nil
55+
}
56+
57+
// Otherwise, just clear it out
58+
var err error
59+
c.httpClient.Jar, err = cookiejar.New(nil)
60+
61+
if err != nil {
62+
return err
63+
}
64+
65+
return nil
66+
}
67+
5068
// request performs an HTTP request with the body provided.
5169
// The caller is responsible for closing the response body.
5270
func (c *Client) request(ctx context.Context, method, path string, body interface{}) (*http.Response, error) {

codersdk/client_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package codersdk_test
2+
3+
import (
4+
"net/url"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/coder/coder/codersdk"
10+
)
11+
12+
var (
13+
serverURL = &url.URL{
14+
Scheme: "https",
15+
Host: "coder.com",
16+
}
17+
)
18+
19+
func TestClient(t *testing.T) {
20+
t.Parallel()
21+
t.Run("SetClearSessionToken", func(t *testing.T) {
22+
t.Parallel()
23+
client := codersdk.New(serverURL)
24+
err := client.SetSessionToken("test-session")
25+
require.NoError(t, err, "Setting a session token should be successful")
26+
27+
err = client.ClearSessionToken()
28+
require.NoError(t, err, "Clearing a session token should be successful")
29+
})
30+
}

codersdk/users.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ func (c *Client) LoginWithPassword(ctx context.Context, req coderd.LoginWithPass
4444
return resp, nil
4545
}
4646

47+
// Logout calls the /logout API
48+
// Call `ClearSessionToken()` to clear the session token of the client.
49+
func (c *Client) Logout(ctx context.Context) error {
50+
// Since `LoginWithPassword` doesn't actually set a SessionToken
51+
// (it requires a call to SetSessionToken), this is essentially a no-op
52+
res, err := c.request(ctx, http.MethodPost, "/api/v2/logout", nil)
53+
if err != nil {
54+
return err
55+
}
56+
defer res.Body.Close()
57+
return nil
58+
}
59+
4760
// User returns a user for the ID provided.
4861
// If the ID string is empty, the current user will be returned.
4962
func (c *Client) User(ctx context.Context, id string) (coderd.User, error) {

codersdk/users_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,12 @@ func TestUsers(t *testing.T) {
4747
require.NoError(t, err)
4848
require.Len(t, orgs, 1)
4949
})
50+
51+
t.Run("LogoutIsSuccessful", func(t *testing.T) {
52+
t.Parallel()
53+
server := coderdtest.New(t)
54+
_ = server.RandomInitialUser(t)
55+
err := server.Client.Logout(context.Background())
56+
require.NoError(t, err)
57+
})
5058
}

0 commit comments

Comments
 (0)