Skip to content

Commit 030bf3f

Browse files
committed
Fix requested changes
1 parent 359f978 commit 030bf3f

File tree

6 files changed

+33
-22
lines changed

6 files changed

+33
-22
lines changed

coderd/coderd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func New(options *Options) http.Handler {
4040
httpmw.ExtractAPIKey(options.Database, nil),
4141
httpmw.ExtractUserParam(options.Database),
4242
)
43-
r.Get("/{user}", users.getUser)
44-
r.Get("/{user}/organizations", users.getUserOrganizations)
43+
r.Get("/{user}", users.user)
44+
r.Get("/{user}/organizations", users.userOrganizations)
4545
})
4646
})
4747
})

coderd/organizations.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import (
99
// Organization is the JSON representation of a Coder organization.
1010
type Organization struct {
1111
ID string `json:"id" validate:"required"`
12-
Username string `json:"username" validate:"required"`
12+
Name string `json:"name" validate:"required"`
1313
CreatedAt time.Time `json:"created_at" validate:"required"`
1414
UpdatedAt time.Time `json:"updated_at" validate:"required"`
1515
}
1616

17-
// convertOrganization consumes the database representation and outputs API friendly.
17+
// convertOrganization consumes the database representation and outputs an API friendly representation.
1818
func convertOrganization(organization database.Organization) Organization {
1919
return Organization{
2020
ID: organization.ID,
21-
Username: organization.Name,
21+
Name: organization.Name,
2222
CreatedAt: organization.CreatedAt,
2323
UpdatedAt: organization.UpdatedAt,
2424
}

coderd/users.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ func (users *users) createInitialUser(rw http.ResponseWriter, r *http.Request) {
110110
UpdatedAt: database.Now(),
111111
Roles: []string{"organization-admin"},
112112
})
113+
if err != nil {
114+
return xerrors.Errorf("create organization member: %w", err)
115+
}
113116
return nil
114117
})
115118
if err != nil {
@@ -123,8 +126,9 @@ func (users *users) createInitialUser(rw http.ResponseWriter, r *http.Request) {
123126
render.JSON(rw, r, user)
124127
}
125128

126-
// Returns the currently authenticated user.
127-
func (*users) getUser(rw http.ResponseWriter, r *http.Request) {
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) {
128132
user := httpmw.UserParam(r)
129133

130134
render.JSON(rw, r, User{
@@ -135,10 +139,11 @@ func (*users) getUser(rw http.ResponseWriter, r *http.Request) {
135139
})
136140
}
137141

138-
func (u *users) getUserOrganizations(rw http.ResponseWriter, r *http.Request) {
142+
// Returns organizations the parameterized user has access to.
143+
func (users *users) userOrganizations(rw http.ResponseWriter, r *http.Request) {
139144
user := httpmw.UserParam(r)
140145

141-
organizations, err := u.Database.GetOrganizationsByUserID(r.Context(), user.ID)
146+
organizations, err := users.Database.GetOrganizationsByUserID(r.Context(), user.ID)
142147
if errors.Is(err, sql.ErrNoRows) {
143148
err = nil
144149
}

database/databasefake/databasefake.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (q *fakeQuerier) GetUserCount(_ context.Context) (int64, error) {
6161
return int64(len(q.users)), nil
6262
}
6363

64-
func (q *fakeQuerier) GetOrganizationByName(ctx context.Context, name string) (database.Organization, error) {
64+
func (q *fakeQuerier) GetOrganizationByName(_ context.Context, name string) (database.Organization, error) {
6565
for _, organization := range q.organizations {
6666
if organization.Name == name {
6767
return organization, nil
@@ -70,7 +70,7 @@ func (q *fakeQuerier) GetOrganizationByName(ctx context.Context, name string) (d
7070
return database.Organization{}, sql.ErrNoRows
7171
}
7272

73-
func (q *fakeQuerier) GetOrganizationsByUserID(ctx context.Context, userID string) ([]database.Organization, error) {
73+
func (q *fakeQuerier) GetOrganizationsByUserID(_ context.Context, userID string) ([]database.Organization, error) {
7474
organizations := make([]database.Organization, 0)
7575
for _, organizationMember := range q.organizationMembers {
7676
if organizationMember.UserID != userID {
@@ -112,7 +112,7 @@ func (q *fakeQuerier) InsertAPIKey(_ context.Context, arg database.InsertAPIKeyP
112112
return key, nil
113113
}
114114

115-
func (q *fakeQuerier) InsertOrganization(ctx context.Context, arg database.InsertOrganizationParams) (database.Organization, error) {
115+
func (q *fakeQuerier) InsertOrganization(_ context.Context, arg database.InsertOrganizationParams) (database.Organization, error) {
116116
organization := database.Organization{
117117
ID: arg.ID,
118118
Name: arg.Name,
@@ -123,7 +123,8 @@ func (q *fakeQuerier) InsertOrganization(ctx context.Context, arg database.Inser
123123
return organization, nil
124124
}
125125

126-
func (q *fakeQuerier) InsertOrganizationMember(ctx context.Context, arg database.InsertOrganizationMemberParams) (database.OrganizationMember, error) {
126+
func (q *fakeQuerier) InsertOrganizationMember(_ context.Context, arg database.InsertOrganizationMemberParams) (database.OrganizationMember, error) {
127+
//nolint:gosimple
127128
organizationMember := database.OrganizationMember{
128129
OrganizationID: arg.OrganizationID,
129130
UserID: arg.UserID,

httpmw/userparam.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
"fmt"
66
"net/http"
77

8+
"github.com/go-chi/chi"
9+
810
"github.com/coder/coder/database"
911
"github.com/coder/coder/httpapi"
10-
"github.com/go-chi/chi"
1112
)
1213

1314
type userParamContextKey struct{}

httpmw/userparam_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/go-chi/chi"
13+
"github.com/stretchr/testify/require"
14+
1215
"github.com/coder/coder/database"
1316
"github.com/coder/coder/database/databasefake"
1417
"github.com/coder/coder/httpmw"
15-
"github.com/go-chi/chi"
16-
"github.com/stretchr/testify/require"
1718
)
1819

1920
func TestUserParam(t *testing.T) {
@@ -55,8 +56,9 @@ func TestUserParam(t *testing.T) {
5556
httpmw.ExtractUserParam(db)(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
5657
rw.WriteHeader(http.StatusOK)
5758
})).ServeHTTP(rw, r)
58-
59-
require.Equal(t, http.StatusBadRequest, rw.Result().StatusCode)
59+
res := rw.Result()
60+
defer res.Body.Close()
61+
require.Equal(t, http.StatusBadRequest, res.StatusCode)
6062
})
6163

6264
t.Run("NotMe", func(t *testing.T) {
@@ -72,8 +74,9 @@ func TestUserParam(t *testing.T) {
7274
httpmw.ExtractUserParam(db)(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
7375
rw.WriteHeader(http.StatusOK)
7476
})).ServeHTTP(rw, r)
75-
76-
require.Equal(t, http.StatusBadRequest, rw.Result().StatusCode)
77+
res := rw.Result()
78+
defer res.Body.Close()
79+
require.Equal(t, http.StatusBadRequest, res.StatusCode)
7780
})
7881

7982
t.Run("Me", func(t *testing.T) {
@@ -89,7 +92,8 @@ func TestUserParam(t *testing.T) {
8992
httpmw.ExtractUserParam(db)(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
9093
rw.WriteHeader(http.StatusOK)
9194
})).ServeHTTP(rw, r)
92-
93-
require.Equal(t, http.StatusOK, rw.Result().StatusCode)
95+
res := rw.Result()
96+
defer res.Body.Close()
97+
require.Equal(t, http.StatusOK, res.StatusCode)
9498
})
9599
}

0 commit comments

Comments
 (0)