Skip to content

Commit 8be2456

Browse files
authored
feat: Add organizations endpoint for users (#50)
* 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. * Fix requested changes * Fix tests * Fix timeout * Add test for UserOrgs * Add test for userparam getting * Add test for NoUser
1 parent 50d8151 commit 8be2456

20 files changed

+597
-244
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.user)
44+
r.Get("/{user}/organizations", users.userOrganizations)
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
@@ -13,6 +13,7 @@ import (
1313
"cdr.dev/slog/sloggers/slogtest"
1414
"github.com/coder/coder/coderd"
1515
"github.com/coder/coder/codersdk"
16+
"github.com/coder/coder/cryptorand"
1617
"github.com/coder/coder/database"
1718
"github.com/coder/coder/database/databasefake"
1819
"github.com/coder/coder/database/postgres"
@@ -27,7 +28,37 @@ type Server struct {
2728
URL *url.URL
2829
}
2930

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

57-
client := codersdk.New(serverURL)
58-
_, err = client.CreateInitialUser(context.Background(), coderd.CreateUserRequest{
59-
Email: "testuser@coder.com",
60-
Username: "testuser",
61-
Password: "testpassword",
62-
})
63-
require.NoError(t, err)
64-
65-
login, err := client.LoginWithPassword(context.Background(), coderd.LoginWithPasswordRequest{
66-
Email: "testuser@coder.com",
67-
Password: "testpassword",
68-
})
69-
require.NoError(t, err)
70-
err = client.SetSessionToken(login.SessionToken)
71-
require.NoError(t, err)
72-
7388
return Server{
74-
Client: client,
89+
Client: codersdk.New(serverURL),
7590
URL: serverURL,
7691
}
7792
}

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+
Name string `json:"name" 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 an API friendly representation.
18+
func convertOrganization(organization database.Organization) Organization {
19+
return Organization{
20+
ID: organization.ID,
21+
Name: organization.Name,
22+
CreatedAt: organization.CreatedAt,
23+
UpdatedAt: organization.UpdatedAt,
24+
}
25+
}

coderd/users.go

Lines changed: 70 additions & 32 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,57 @@ 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+
if err != nil {
114+
return xerrors.Errorf("create organization member: %w", err)
115+
}
116+
return nil
102117
})
103118
if err != nil {
104119
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
105-
Message: fmt.Sprintf("create user: %s", err.Error()),
120+
Message: err.Error(),
106121
})
107122
return
108123
}
124+
109125
render.Status(r, http.StatusCreated)
110126
render.JSON(rw, r, user)
111127
}
112128

113-
// Returns the currently authenticated user.
114-
func (*users) authenticatedUser(rw http.ResponseWriter, r *http.Request) {
115-
user := httpmw.User(r)
129+
// Returns the parameterized user requested. All validation
130+
// is completed in the middleware for this route.
131+
func (*users) user(rw http.ResponseWriter, r *http.Request) {
132+
user := httpmw.UserParam(r)
116133

117134
render.JSON(rw, r, User{
118135
ID: user.ID,
@@ -122,6 +139,27 @@ func (*users) authenticatedUser(rw http.ResponseWriter, r *http.Request) {
122139
})
123140
}
124141

142+
// Returns organizations the parameterized user has access to.
143+
func (users *users) userOrganizations(rw http.ResponseWriter, r *http.Request) {
144+
user := httpmw.UserParam(r)
145+
146+
organizations, err := users.Database.GetOrganizationsByUserID(r.Context(), user.ID)
147+
if err != nil {
148+
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
149+
Message: fmt.Sprintf("get organizations: %s", err.Error()),
150+
})
151+
return
152+
}
153+
154+
publicOrganizations := make([]Organization, 0, len(organizations))
155+
for _, organization := range organizations {
156+
publicOrganizations = append(publicOrganizations, convertOrganization(organization))
157+
}
158+
159+
render.Status(r, http.StatusOK)
160+
render.JSON(rw, r, publicOrganizations)
161+
}
162+
125163
// Authenticates the user with an email and password.
126164
func (users *users) loginWithPassword(rw http.ResponseWriter, r *http.Request) {
127165
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)