Skip to content

Commit 359f978

Browse files
committed
feat: Add organizations endpoint for users
This moves the /user endpoint to /users/me instead. This will reduce code duplication. This adds /users/<name>/organizations to list organizations a user has access to. It doesn't contain the permissions a user has over the organizations, but that will come in a future contribution.
1 parent 4183a4e commit 359f978

File tree

20 files changed

+569
-235
lines changed

20 files changed

+569
-235
lines changed

codecov.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ ignore:
2323
# This is generated code.
2424
- database/models.go
2525
- database/query.sql.go
26+
# All coderd tests fail if this doesn't work.
27+
- database/databasefake
2628
- peerbroker/proto
2729
- provisionersdk/proto

coderd/coderd.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,18 @@ func New(options *Options) http.Handler {
3131
Message: "👋",
3232
})
3333
})
34-
r.Post("/user", users.createInitialUser)
3534
r.Post("/login", users.loginWithPassword)
36-
// Require an API key and authenticated user for this group.
37-
r.Group(func(r chi.Router) {
38-
r.Use(
39-
httpmw.ExtractAPIKey(options.Database, nil),
40-
httpmw.ExtractUser(options.Database),
41-
)
42-
r.Get("/user", users.authenticatedUser)
35+
r.Route("/users", func(r chi.Router) {
36+
r.Post("/", users.createInitialUser)
37+
38+
r.Group(func(r chi.Router) {
39+
r.Use(
40+
httpmw.ExtractAPIKey(options.Database, nil),
41+
httpmw.ExtractUserParam(options.Database),
42+
)
43+
r.Get("/{user}", users.getUser)
44+
r.Get("/{user}/organizations", users.getUserOrganizations)
45+
})
4346
})
4447
})
4548
r.NotFound(site.Handler().ServeHTTP)

coderd/coderdtest/coderdtest.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"cdr.dev/slog/sloggers/slogtest"
1212
"github.com/coder/coder/coderd"
1313
"github.com/coder/coder/codersdk"
14+
"github.com/coder/coder/cryptorand"
1415
"github.com/coder/coder/database/databasefake"
1516
)
1617

@@ -23,7 +24,37 @@ type Server struct {
2324
URL *url.URL
2425
}
2526

26-
// New constructs a new coderd test instance.
27+
// RandomInitialUser generates a random initial user and authenticates
28+
// it with the client on the Server struct.
29+
func (s *Server) RandomInitialUser(t *testing.T) coderd.CreateInitialUserRequest {
30+
username, err := cryptorand.String(12)
31+
require.NoError(t, err)
32+
password, err := cryptorand.String(12)
33+
require.NoError(t, err)
34+
organization, err := cryptorand.String(12)
35+
require.NoError(t, err)
36+
37+
req := coderd.CreateInitialUserRequest{
38+
Email: "testuser@coder.com",
39+
Username: username,
40+
Password: password,
41+
Organization: organization,
42+
}
43+
_, err = s.Client.CreateInitialUser(context.Background(), req)
44+
require.NoError(t, err)
45+
46+
login, err := s.Client.LoginWithPassword(context.Background(), coderd.LoginWithPasswordRequest{
47+
Email: "testuser@coder.com",
48+
Password: password,
49+
})
50+
require.NoError(t, err)
51+
err = s.Client.SetSessionToken(login.SessionToken)
52+
require.NoError(t, err)
53+
return req
54+
}
55+
56+
// New constructs a new coderd test instance. This returned Server
57+
// should contain no side-effects.
2758
func New(t *testing.T) Server {
2859
// This can be hotswapped for a live database instance.
2960
db := databasefake.New()
@@ -36,24 +67,8 @@ func New(t *testing.T) Server {
3667
require.NoError(t, err)
3768
t.Cleanup(srv.Close)
3869

39-
client := codersdk.New(serverURL)
40-
_, err = client.CreateInitialUser(context.Background(), coderd.CreateUserRequest{
41-
Email: "testuser@coder.com",
42-
Username: "testuser",
43-
Password: "testpassword",
44-
})
45-
require.NoError(t, err)
46-
47-
login, err := client.LoginWithPassword(context.Background(), coderd.LoginWithPasswordRequest{
48-
Email: "testuser@coder.com",
49-
Password: "testpassword",
50-
})
51-
require.NoError(t, err)
52-
err = client.SetSessionToken(login.SessionToken)
53-
require.NoError(t, err)
54-
5570
return Server{
56-
Client: client,
71+
Client: codersdk.New(serverURL),
5772
URL: serverURL,
5873
}
5974
}

coderd/coderdtest/coderdtest_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ func TestMain(m *testing.M) {
1313
}
1414

1515
func TestNew(t *testing.T) {
16-
_ = coderdtest.New(t)
16+
server := coderdtest.New(t)
17+
_ = server.RandomInitialUser(t)
1718
}

coderd/organizations.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package coderd
2+
3+
import (
4+
"time"
5+
6+
"github.com/coder/coder/database"
7+
)
8+
9+
// Organization is the JSON representation of a Coder organization.
10+
type Organization struct {
11+
ID string `json:"id" validate:"required"`
12+
Username string `json:"username" validate:"required"`
13+
CreatedAt time.Time `json:"created_at" validate:"required"`
14+
UpdatedAt time.Time `json:"updated_at" validate:"required"`
15+
}
16+
17+
// convertOrganization consumes the database representation and outputs API friendly.
18+
func convertOrganization(organization database.Organization) Organization {
19+
return Organization{
20+
ID: organization.ID,
21+
Username: organization.Name,
22+
CreatedAt: organization.CreatedAt,
23+
UpdatedAt: organization.UpdatedAt,
24+
}
25+
}

coderd/users.go

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package coderd
22

33
import (
4-
"context"
54
"crypto/sha256"
65
"database/sql"
76
"errors"
@@ -11,6 +10,7 @@ import (
1110

1211
"github.com/go-chi/render"
1312
"github.com/google/uuid"
13+
"golang.org/x/xerrors"
1414

1515
"github.com/coder/coder/coderd/userpassword"
1616
"github.com/coder/coder/cryptorand"
@@ -27,11 +27,12 @@ type User struct {
2727
Username string `json:"username" validate:"required"`
2828
}
2929

30-
// CreateUserRequest enables callers to create a new user.
31-
type CreateUserRequest struct {
32-
Email string `json:"email" validate:"required,email"`
33-
Username string `json:"username" validate:"required,username"`
34-
Password string `json:"password" validate:"required"`
30+
// CreateInitialUserRequest enables callers to create a new user.
31+
type CreateInitialUserRequest struct {
32+
Email string `json:"email" validate:"required,email"`
33+
Username string `json:"username" validate:"required,username"`
34+
Password string `json:"password" validate:"required"`
35+
Organization string `json:"organization" validate:"required,username"`
3536
}
3637

3738
// LoginWithPasswordRequest enables callers to authenticate with email and password.
@@ -51,7 +52,7 @@ type users struct {
5152

5253
// Creates the initial user for a Coder deployment.
5354
func (users *users) createInitialUser(rw http.ResponseWriter, r *http.Request) {
54-
var createUser CreateUserRequest
55+
var createUser CreateInitialUserRequest
5556
if !httpapi.Read(rw, r, &createUser) {
5657
return
5758
}
@@ -70,19 +71,6 @@ func (users *users) createInitialUser(rw http.ResponseWriter, r *http.Request) {
7071
})
7172
return
7273
}
73-
_, err = users.Database.GetUserByEmailOrUsername(r.Context(), database.GetUserByEmailOrUsernameParams{
74-
Email: createUser.Email,
75-
Username: createUser.Username,
76-
})
77-
if errors.Is(err, sql.ErrNoRows) {
78-
err = nil
79-
}
80-
if err != nil {
81-
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
82-
Message: fmt.Sprintf("get user: %s", err.Error()),
83-
})
84-
return
85-
}
8674
hashedPassword, err := userpassword.Hash(createUser.Password)
8775
if err != nil {
8876
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
@@ -91,28 +79,53 @@ func (users *users) createInitialUser(rw http.ResponseWriter, r *http.Request) {
9179
return
9280
}
9381

94-
user, err := users.Database.InsertUser(context.Background(), database.InsertUserParams{
95-
ID: uuid.NewString(),
96-
Email: createUser.Email,
97-
HashedPassword: []byte(hashedPassword),
98-
Username: createUser.Username,
99-
LoginType: database.LoginTypeBuiltIn,
100-
CreatedAt: database.Now(),
101-
UpdatedAt: database.Now(),
82+
// Create the user, organization, and membership to the user.
83+
var user database.User
84+
err = users.Database.InTx(func(s database.Store) error {
85+
user, err = users.Database.InsertUser(r.Context(), database.InsertUserParams{
86+
ID: uuid.NewString(),
87+
Email: createUser.Email,
88+
HashedPassword: []byte(hashedPassword),
89+
Username: createUser.Username,
90+
LoginType: database.LoginTypeBuiltIn,
91+
CreatedAt: database.Now(),
92+
UpdatedAt: database.Now(),
93+
})
94+
if err != nil {
95+
return xerrors.Errorf("create user: %w", err)
96+
}
97+
organization, err := users.Database.InsertOrganization(r.Context(), database.InsertOrganizationParams{
98+
ID: uuid.NewString(),
99+
Name: createUser.Organization,
100+
CreatedAt: database.Now(),
101+
UpdatedAt: database.Now(),
102+
})
103+
if err != nil {
104+
return xerrors.Errorf("create organization: %w", err)
105+
}
106+
_, err = users.Database.InsertOrganizationMember(r.Context(), database.InsertOrganizationMemberParams{
107+
OrganizationID: organization.ID,
108+
UserID: user.ID,
109+
CreatedAt: database.Now(),
110+
UpdatedAt: database.Now(),
111+
Roles: []string{"organization-admin"},
112+
})
113+
return nil
102114
})
103115
if err != nil {
104116
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
105-
Message: fmt.Sprintf("create user: %s", err.Error()),
117+
Message: err.Error(),
106118
})
107119
return
108120
}
121+
109122
render.Status(r, http.StatusCreated)
110123
render.JSON(rw, r, user)
111124
}
112125

113126
// Returns the currently authenticated user.
114-
func (*users) authenticatedUser(rw http.ResponseWriter, r *http.Request) {
115-
user := httpmw.User(r)
127+
func (*users) getUser(rw http.ResponseWriter, r *http.Request) {
128+
user := httpmw.UserParam(r)
116129

117130
render.JSON(rw, r, User{
118131
ID: user.ID,
@@ -122,6 +135,29 @@ func (*users) authenticatedUser(rw http.ResponseWriter, r *http.Request) {
122135
})
123136
}
124137

138+
func (u *users) getUserOrganizations(rw http.ResponseWriter, r *http.Request) {
139+
user := httpmw.UserParam(r)
140+
141+
organizations, err := u.Database.GetOrganizationsByUserID(r.Context(), user.ID)
142+
if errors.Is(err, sql.ErrNoRows) {
143+
err = nil
144+
}
145+
if err != nil {
146+
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
147+
Message: fmt.Sprintf("get organizations: %s", err.Error()),
148+
})
149+
return
150+
}
151+
152+
publicOrganizations := make([]Organization, 0, len(organizations))
153+
for _, organization := range organizations {
154+
publicOrganizations = append(publicOrganizations, convertOrganization(organization))
155+
}
156+
157+
render.Status(r, http.StatusOK)
158+
render.JSON(rw, r, publicOrganizations)
159+
}
160+
125161
// Authenticates the user with an email and password.
126162
func (users *users) loginWithPassword(rw http.ResponseWriter, r *http.Request) {
127163
var loginWithPassword LoginWithPasswordRequest

coderd/users_test.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,35 @@ func TestUsers(t *testing.T) {
1616
t.Run("Authenticated", func(t *testing.T) {
1717
t.Parallel()
1818
server := coderdtest.New(t)
19+
_ = server.RandomInitialUser(t)
1920
_, err := server.Client.User(context.Background(), "")
2021
require.NoError(t, err)
2122
})
2223

2324
t.Run("CreateMultipleInitial", func(t *testing.T) {
2425
t.Parallel()
2526
server := coderdtest.New(t)
26-
_, err := server.Client.CreateInitialUser(context.Background(), coderd.CreateUserRequest{
27-
Email: "dummy@coder.com",
28-
Username: "fake",
29-
Password: "password",
27+
_ = server.RandomInitialUser(t)
28+
_, err := server.Client.CreateInitialUser(context.Background(), coderd.CreateInitialUserRequest{
29+
Email: "dummy@coder.com",
30+
Organization: "bananas",
31+
Username: "fake",
32+
Password: "password",
3033
})
3134
require.Error(t, err)
3235
})
3336

34-
t.Run("LoginNoEmail", func(t *testing.T) {
37+
t.Run("Login", func(t *testing.T) {
38+
server := coderdtest.New(t)
39+
user := server.RandomInitialUser(t)
40+
_, err := server.Client.LoginWithPassword(context.Background(), coderd.LoginWithPasswordRequest{
41+
Email: user.Email,
42+
Password: user.Password,
43+
})
44+
require.NoError(t, err)
45+
})
46+
47+
t.Run("LoginInvalidUser", func(t *testing.T) {
3548
t.Parallel()
3649
server := coderdtest.New(t)
3750
_, err := server.Client.LoginWithPassword(context.Background(), coderd.LoginWithPasswordRequest{
@@ -44,13 +57,20 @@ func TestUsers(t *testing.T) {
4457
t.Run("LoginBadPassword", func(t *testing.T) {
4558
t.Parallel()
4659
server := coderdtest.New(t)
47-
user, err := server.Client.User(context.Background(), "")
48-
require.NoError(t, err)
49-
50-
_, err = server.Client.LoginWithPassword(context.Background(), coderd.LoginWithPasswordRequest{
60+
user := server.RandomInitialUser(t)
61+
_, err := server.Client.LoginWithPassword(context.Background(), coderd.LoginWithPasswordRequest{
5162
Email: user.Email,
5263
Password: "bananas",
5364
})
5465
require.Error(t, err)
5566
})
67+
68+
t.Run("ListOrganizations", func(t *testing.T) {
69+
t.Parallel()
70+
server := coderdtest.New(t)
71+
_ = server.RandomInitialUser(t)
72+
orgs, err := server.Client.UserOrganizations(context.Background(), "")
73+
require.NoError(t, err)
74+
require.Len(t, orgs, 1)
75+
})
5676
}

0 commit comments

Comments
 (0)