Skip to content

Commit 3904a07

Browse files
committed
fix dbmem and add additional tests
1 parent 6e2ffd1 commit 3904a07

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func New() database.Store {
8686
defaultOrg, err := q.InsertOrganization(context.Background(), database.InsertOrganizationParams{
8787
ID: uuid.New(),
8888
Name: "first-organization",
89+
DisplayName: "first-organization",
8990
Description: "Builtin default organization.",
9091
CreatedAt: dbtime.Now(),
9192
UpdatedAt: dbtime.Now(),
@@ -6177,11 +6178,13 @@ func (q *FakeQuerier) InsertOrganization(_ context.Context, arg database.InsertO
61776178
defer q.mutex.Unlock()
61786179

61796180
organization := database.Organization{
6180-
ID: arg.ID,
6181-
Name: arg.Name,
6182-
CreatedAt: arg.CreatedAt,
6183-
UpdatedAt: arg.UpdatedAt,
6184-
IsDefault: len(q.organizations) == 0,
6181+
ID: arg.ID,
6182+
Name: arg.Name,
6183+
DisplayName: arg.DisplayName,
6184+
Description: arg.Description,
6185+
CreatedAt: arg.CreatedAt,
6186+
UpdatedAt: arg.UpdatedAt,
6187+
IsDefault: len(q.organizations) == 0,
61856188
}
61866189
q.organizations = append(q.organizations, organization)
61876190
return organization, nil
@@ -7322,6 +7325,8 @@ func (q *FakeQuerier) UpdateOrganization(_ context.Context, arg database.UpdateO
73227325
for i, org := range q.organizations {
73237326
if org.ID == arg.ID {
73247327
org.Name = arg.Name
7328+
org.DisplayName = arg.DisplayName
7329+
org.Description = arg.Description
73257330
q.organizations[i] = org
73267331
return org, nil
73277332
}

coderd/organizations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ func (api *API) postOrganizations(rw http.ResponseWriter, r *http.Request) {
7878
ID: uuid.New(),
7979
Name: req.Name,
8080
DisplayName: req.DisplayName,
81+
Description: req.Description,
8182
CreatedAt: dbtime.Now(),
8283
UpdatedAt: dbtime.Now(),
83-
Description: "",
8484
})
8585
if err != nil {
8686
return xerrors.Errorf("create organization: %w", err)

coderd/organizations_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,36 @@ func TestPostOrganizationsByUser(t *testing.T) {
117117
require.Equal(t, http.StatusConflict, apiErr.StatusCode())
118118
})
119119

120-
t.Run("Create", func(t *testing.T) {
120+
t.Run("InvalidName", func(t *testing.T) {
121121
t.Parallel()
122122
client := coderdtest.New(t, nil)
123123
_ = coderdtest.CreateFirstUser(t, client)
124124
ctx := testutil.Context(t, testutil.WaitLong)
125125

126126
_, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
127+
Name: "A name which is definitely! not! url! safe!",
128+
DisplayName: "New",
129+
})
130+
var apiErr *codersdk.Error
131+
require.ErrorAs(t, err, &apiErr)
132+
require.Equal(t, http.StatusBadRequest, apiErr.StatusCode())
133+
})
134+
135+
t.Run("Create", func(t *testing.T) {
136+
t.Parallel()
137+
client := coderdtest.New(t, nil)
138+
_ = coderdtest.CreateFirstUser(t, client)
139+
ctx := testutil.Context(t, testutil.WaitLong)
140+
141+
o, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
127142
Name: "new",
128143
DisplayName: "New",
144+
Description: "A new organization to love and cherish forever.",
129145
})
130146
require.NoError(t, err)
147+
require.Equal(t, "new", o.Name)
148+
require.Equal(t, "New", o.DisplayName)
149+
require.Equal(t, "A new organization to love and cherish forever.", o.Description)
131150
})
132151
}
133152

@@ -175,6 +194,26 @@ func TestPatchOrganizationsByUser(t *testing.T) {
175194
require.Equal(t, http.StatusBadRequest, apiErr.StatusCode())
176195
})
177196

197+
t.Run("InvalidName", func(t *testing.T) {
198+
t.Parallel()
199+
client := coderdtest.New(t, nil)
200+
_ = coderdtest.CreateFirstUser(t, client)
201+
ctx := testutil.Context(t, testutil.WaitMedium)
202+
203+
o, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
204+
Name: "something-unique",
205+
DisplayName: "Something Unique",
206+
})
207+
require.NoError(t, err)
208+
209+
_, err = client.UpdateOrganization(ctx, o.ID.String(), codersdk.UpdateOrganizationRequest{
210+
Name: "something! unique! but not! url! safe!",
211+
})
212+
var apiErr *codersdk.Error
213+
require.ErrorAs(t, err, &apiErr)
214+
require.Equal(t, http.StatusBadRequest, apiErr.StatusCode())
215+
})
216+
178217
t.Run("UpdateById", func(t *testing.T) {
179218
t.Parallel()
180219
client := coderdtest.New(t, nil)

codersdk/organizations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type OrganizationMember struct {
6060
type CreateOrganizationRequest struct {
6161
Name string `json:"name" validate:"required,username"`
6262
DisplayName string `json:"display_name" validate:"required"`
63+
Description string `json:"description"`
6364
}
6465

6566
type UpdateOrganizationRequest struct {

0 commit comments

Comments
 (0)