Skip to content

Commit 1188ff0

Browse files
committed
Merge master
2 parents a05f476 + a96cd3f commit 1188ff0

25 files changed

+669
-265
lines changed

.eslintrc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ rules:
3838
"@typescript-eslint/explicit-function-return-type": "off"
3939
"@typescript-eslint/explicit-module-boundary-types": "error"
4040
"@typescript-eslint/method-signature-style": ["error", "property"]
41+
"@typescript-eslint/no-floating-promises": error
4142
"@typescript-eslint/no-invalid-void-type": error
4243
# We're disabling the `no-namespace` rule to use a pattern of defining an interface,
4344
# and then defining functions that operate on that data via namespace. This is helpful for

.github/workflows/coder.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,19 @@ jobs:
149149
150150
- run: go install gotest.tools/gotestsum@latest
151151

152-
- run:
152+
- name: Test with Mock Database
153+
run:
153154
gotestsum --jsonfile="gotests.json" --packages="./..." --
154155
-covermode=atomic -coverprofile="gotests.coverage" -timeout=3m
155156
-count=3 -race -parallel=2
156157

158+
- name: Test with PostgreSQL Database
159+
if: runner.os == 'Linux'
160+
run:
161+
DB=true gotestsum --jsonfile="gotests.json" --packages="./..." --
162+
-covermode=atomic -coverprofile="gotests.coverage" -timeout=3m
163+
-count=1 -race -parallel=2
164+
157165
- uses: codecov/codecov-action@v2
158166
with:
159167
token: ${{ secrets.CODECOV_TOKEN }}

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: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,19 @@ func New(options *Options) http.Handler {
3131
Message: "👋",
3232
})
3333
})
34-
r.Post("/user", users.createInitialUser)
3534
r.Post("/login", users.loginWithPassword)
36-
r.Post("/logout", logout)
37-
// Require an API key and authenticated user for this group.
38-
r.Group(func(r chi.Router) {
39-
r.Use(
40-
httpmw.ExtractAPIKey(options.Database, nil),
41-
httpmw.ExtractUser(options.Database),
42-
)
43-
r.Get("/user", users.authenticatedUser)
35+
r.Post("/logout", users.logout)
36+
r.Route("/users", func(r chi.Router) {
37+
r.Post("/", users.createInitialUser)
38+
39+
r.Group(func(r chi.Router) {
40+
r.Use(
41+
httpmw.ExtractAPIKey(options.Database, nil),
42+
httpmw.ExtractUserParam(options.Database),
43+
)
44+
r.Get("/{user}", users.user)
45+
r.Get("/{user}/organizations", users.userOrganizations)
46+
})
4447
})
4548
})
4649
r.NotFound(site.Handler().ServeHTTP)

coderd/coderdtest/coderdtest.go

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ package coderdtest
22

33
import (
44
"context"
5+
"database/sql"
56
"net/http/httptest"
67
"net/url"
8+
"os"
79
"testing"
810

911
"github.com/stretchr/testify/require"
1012

1113
"cdr.dev/slog/sloggers/slogtest"
1214
"github.com/coder/coder/coderd"
1315
"github.com/coder/coder/codersdk"
16+
"github.com/coder/coder/cryptorand"
17+
"github.com/coder/coder/database"
1418
"github.com/coder/coder/database/databasefake"
19+
"github.com/coder/coder/database/postgres"
1520
)
1621

1722
// Server represents a test instance of coderd.
@@ -23,10 +28,54 @@ type Server struct {
2328
URL *url.URL
2429
}
2530

26-
// 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.
2762
func New(t *testing.T) Server {
2863
// This can be hotswapped for a live database instance.
2964
db := databasefake.New()
65+
if os.Getenv("DB") != "" {
66+
connectionURL, close, err := postgres.Open()
67+
require.NoError(t, err)
68+
t.Cleanup(close)
69+
sqlDB, err := sql.Open("postgres", connectionURL)
70+
require.NoError(t, err)
71+
t.Cleanup(func() {
72+
_ = sqlDB.Close()
73+
})
74+
err = database.Migrate(sqlDB)
75+
require.NoError(t, err)
76+
db = database.New(sqlDB)
77+
}
78+
3079
handler := coderd.New(&coderd.Options{
3180
Logger: slogtest.Make(t, nil),
3281
Database: db,
@@ -36,24 +85,8 @@ func New(t *testing.T) Server {
3685
require.NoError(t, err)
3786
t.Cleanup(srv.Close)
3887

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-
5588
return Server{
56-
Client: client,
89+
Client: codersdk.New(serverURL),
5790
URL: serverURL,
5891
}
5992
}

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: 71 additions & 33 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
@@ -200,7 +238,7 @@ func (users *users) loginWithPassword(rw http.ResponseWriter, r *http.Request) {
200238
}
201239

202240
// Clear the user's session cookie
203-
func logout(rw http.ResponseWriter, r *http.Request) {
241+
func (_ *users) logout(rw http.ResponseWriter, r *http.Request) {
204242
// Get a blank token cookie
205243
cookie := &http.Cookie{
206244
// MaxAge < 0 means to delete the cookie now

0 commit comments

Comments
 (0)