feat: unexpose coderdtest.NewWithAPI (#2613)

* feat: unexpose coderdtest.NewWithAPI
This commit is contained in:
Jon Ayers
2022-06-27 13:50:52 -05:00
committed by GitHub
parent 7dfec821f5
commit 2353687610
18 changed files with 409 additions and 241 deletions
+31
View File
@@ -22,6 +22,13 @@ const (
UserStatusSuspended UserStatus = "suspended"
)
type LoginType string
const (
LoginTypePassword LoginType = "password"
LoginTypeGithub LoginType = "github"
)
type UsersRequest struct {
Search string `json:"search,omitempty" typescript:"-"`
// Filter users by status.
@@ -44,6 +51,17 @@ type User struct {
Roles []Role `json:"roles"`
}
type APIKey struct {
ID string `json:"id" validate:"required"`
UserID uuid.UUID `json:"user_id" validate:"required"`
LastUsed time.Time `json:"last_used" validate:"required"`
ExpiresAt time.Time `json:"expires_at" validate:"required"`
CreatedAt time.Time `json:"created_at" validate:"required"`
UpdatedAt time.Time `json:"updated_at" validate:"required"`
LoginType LoginType `json:"login_type" validate:"required"`
LifetimeSeconds int64 `json:"lifetime_seconds" validate:"required"`
}
type CreateFirstUserRequest struct {
Email string `json:"email" validate:"required,email"`
Username string `json:"username" validate:"required,username"`
@@ -314,6 +332,19 @@ func (c *Client) CreateAPIKey(ctx context.Context, user string) (*GenerateAPIKey
return apiKey, json.NewDecoder(res.Body).Decode(apiKey)
}
func (c *Client) GetAPIKey(ctx context.Context, user string, id string) (*APIKey, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/keys/%s", user, id), nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode > http.StatusCreated {
return nil, readBodyAsError(res)
}
apiKey := &APIKey{}
return apiKey, json.NewDecoder(res.Body).Decode(apiKey)
}
// LoginWithPassword creates a session token authenticating with an email and password.
// Call `SetSessionToken()` to apply the newly acquired token to the client.
func (c *Client) LoginWithPassword(ctx context.Context, req LoginWithPasswordRequest) (LoginWithPasswordResponse, error) {