This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
feat: enable password changes in coder-sdk #251
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package coder_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"cdr.dev/coder-cli/coder-sdk" | ||
) | ||
|
||
func TestPushActivity(t *testing.T) { | ||
t.Parallel() | ||
|
||
const source = "test" | ||
const envID = "602d377a-e6b8d763cae7561885c5f1b2" | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
require.Equal(t, http.MethodPost, r.Method, "PushActivity is a POST") | ||
require.Equal(t, "/api/private/metrics/usage/push", r.URL.Path) | ||
|
||
expected := map[string]interface{}{ | ||
"source": source, | ||
"environment_id": envID, | ||
} | ||
var request map[string]interface{} | ||
err := json.NewDecoder(r.Body).Decode(&request) | ||
require.NoError(t, err, "error decoding JSON") | ||
require.EqualValues(t, expected, request, "unexpected request data") | ||
|
||
w.WriteHeader(http.StatusOK) | ||
})) | ||
t.Cleanup(func() { | ||
server.Close() | ||
}) | ||
|
||
u, err := url.Parse(server.URL) | ||
require.NoError(t, err, "failed to parse test server URL") | ||
|
||
client, err := coder.NewClient(coder.ClientOptions{ | ||
BaseURL: u, | ||
Token: "SwdcSoq5Jc-0C1r8wfwm7h6h9i0RDk7JT", | ||
}) | ||
require.NoError(t, err, "failed to create coder.Client") | ||
|
||
err = client.PushActivity(context.Background(), source, envID) | ||
require.NoError(t, err) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
|
||
"cdr.dev/coder-cli/coder-sdk" | ||
) | ||
|
||
func main() { | ||
client, err := coder.NewClient(coder.ClientOptions{ | ||
BaseURL: &url.URL{ | ||
Scheme: "http", | ||
Host: "localhost:8080", | ||
}, | ||
Email: "admin", | ||
Password: "vt9g9rxsptrq", | ||
}) | ||
if err != nil { | ||
fmt.Printf("login error: %v\n", err) | ||
return | ||
} | ||
|
||
user, err := client.Me(context.Background()) | ||
if err != nil { | ||
fmt.Printf("Me error: %v\n", err) | ||
return | ||
} | ||
|
||
fmt.Printf("user info: %#v\n", user) | ||
|
||
fmt.Println("changing password") | ||
err = client.UpdateUser(context.Background(), "me", coder.UpdateUserReq{ | ||
UserPasswordSettings: &coder.UserPasswordSettings{ | ||
Password: "szbp4q3bcrhc", | ||
}, | ||
}) | ||
if err != nil { | ||
fmt.Printf("password update error: %v\n", err) | ||
return | ||
} | ||
|
||
orgs, err := client.Organizations(context.Background()) | ||
if err != nil { | ||
fmt.Printf("org list error: %v\n", err) | ||
return | ||
} | ||
|
||
fmt.Printf("orgs info: %#v\n", orgs) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package coder_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"cdr.dev/coder-cli/coder-sdk" | ||
) | ||
|
||
func TestUsers(t *testing.T) { | ||
t.Parallel() | ||
|
||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
require.Equal(t, http.MethodGet, r.Method, "Users is a GET") | ||
require.Equal(t, "/api/v0/users", r.URL.Path) | ||
|
||
users := []map[string]interface{}{ | ||
{ | ||
"id": "default", | ||
"email": "root@user.com", | ||
"username": "root", | ||
"name": "Charlie Root", | ||
"roles": []coder.Role{coder.SiteAdmin}, | ||
"temporary_password": false, | ||
"login_type": coder.LoginTypeBuiltIn, | ||
"key_regenerated_at": time.Now(), | ||
"created_at": time.Now(), | ||
"updated_at": time.Now(), | ||
}, | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
err := json.NewEncoder(w).Encode(users) | ||
require.NoError(t, err, "error encoding JSON") | ||
})) | ||
t.Cleanup(func() { | ||
server.Close() | ||
}) | ||
|
||
u, err := url.Parse(server.URL) | ||
require.NoError(t, err, "failed to parse test server URL") | ||
|
||
client, err := coder.NewClient(coder.ClientOptions{ | ||
BaseURL: u, | ||
Token: "JcmErkJjju-KSrztst0IJX7xGJhKQPtfv", | ||
}) | ||
require.NoError(t, err, "failed to create coder.Client") | ||
|
||
users, err := client.Users(context.Background()) | ||
require.NoError(t, err, "error getting Users") | ||
require.Len(t, users, 1, "users should return a single user") | ||
require.Equal(t, "Charlie Root", users[0].Name) | ||
require.Equal(t, "root", users[0].Username) | ||
} | ||
|
||
func TestUserUpdatePassword(t *testing.T) { | ||
t.Parallel() | ||
|
||
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
require.Equal(t, http.MethodPatch, r.Method, "Users is a PATCH") | ||
require.Equal(t, "/api/v0/users/me", r.URL.Path) | ||
|
||
expected := map[string]interface{}{ | ||
"old_password": "vt9g9rxsptrq", | ||
"password": "wmf39jw2f7pk", | ||
"temporary_password": true, | ||
} | ||
var request map[string]interface{} | ||
err := json.NewDecoder(r.Body).Decode(&request) | ||
require.NoError(t, err, "error decoding JSON") | ||
require.EqualValues(t, expected, request, "unexpected request data") | ||
|
||
w.WriteHeader(http.StatusOK) | ||
})) | ||
t.Cleanup(func() { | ||
server.Close() | ||
}) | ||
|
||
u, err := url.Parse(server.URL) | ||
require.NoError(t, err, "failed to parse test server URL") | ||
|
||
client, err := coder.NewClient(coder.ClientOptions{ | ||
BaseURL: u, | ||
HTTPClient: server.Client(), | ||
Token: "JcmErkJjju-KSrztst0IJX7xGJhKQPtfv", | ||
}) | ||
require.NoError(t, err, "failed to create coder.Client") | ||
|
||
err = client.UpdateUser(context.Background(), "me", coder.UpdateUserReq{ | ||
UserPasswordSettings: &coder.UserPasswordSettings{ | ||
OldPassword: "vt9g9rxsptrq", | ||
Password: "wmf39jw2f7pk", | ||
Temporary: true, | ||
jawnsy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}) | ||
require.NoError(t, err, "error when updating password") | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.