Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix dbmem and add additional tests
  • Loading branch information
aslilac committed Jun 5, 2024
commit 3904a0775740a033a5b90c3d1a56e7387c983623
15 changes: 10 additions & 5 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func New() database.Store {
defaultOrg, err := q.InsertOrganization(context.Background(), database.InsertOrganizationParams{
ID: uuid.New(),
Name: "first-organization",
DisplayName: "first-organization",
Description: "Builtin default organization.",
CreatedAt: dbtime.Now(),
UpdatedAt: dbtime.Now(),
Expand Down Expand Up @@ -6177,11 +6178,13 @@ func (q *FakeQuerier) InsertOrganization(_ context.Context, arg database.InsertO
defer q.mutex.Unlock()

organization := database.Organization{
ID: arg.ID,
Name: arg.Name,
CreatedAt: arg.CreatedAt,
UpdatedAt: arg.UpdatedAt,
IsDefault: len(q.organizations) == 0,
ID: arg.ID,
Name: arg.Name,
DisplayName: arg.DisplayName,
Description: arg.Description,
CreatedAt: arg.CreatedAt,
UpdatedAt: arg.UpdatedAt,
IsDefault: len(q.organizations) == 0,
}
q.organizations = append(q.organizations, organization)
return organization, nil
Expand Down Expand Up @@ -7322,6 +7325,8 @@ func (q *FakeQuerier) UpdateOrganization(_ context.Context, arg database.UpdateO
for i, org := range q.organizations {
if org.ID == arg.ID {
org.Name = arg.Name
org.DisplayName = arg.DisplayName
org.Description = arg.Description
q.organizations[i] = org
return org, nil
}
Expand Down
2 changes: 1 addition & 1 deletion coderd/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func (api *API) postOrganizations(rw http.ResponseWriter, r *http.Request) {
ID: uuid.New(),
Name: req.Name,
DisplayName: req.DisplayName,
Description: req.Description,
CreatedAt: dbtime.Now(),
UpdatedAt: dbtime.Now(),
Description: "",
})
if err != nil {
return xerrors.Errorf("create organization: %w", err)
Expand Down
41 changes: 40 additions & 1 deletion coderd/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,36 @@ func TestPostOrganizationsByUser(t *testing.T) {
require.Equal(t, http.StatusConflict, apiErr.StatusCode())
})

t.Run("Create", func(t *testing.T) {
t.Run("InvalidName", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
ctx := testutil.Context(t, testutil.WaitLong)

_, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "A name which is definitely! not! url! safe!",
DisplayName: "New",
})
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusBadRequest, apiErr.StatusCode())
})

t.Run("Create", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
ctx := testutil.Context(t, testutil.WaitLong)

o, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "new",
DisplayName: "New",
Description: "A new organization to love and cherish forever.",
})
require.NoError(t, err)
require.Equal(t, "new", o.Name)
require.Equal(t, "New", o.DisplayName)
require.Equal(t, "A new organization to love and cherish forever.", o.Description)
})
}

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

t.Run("InvalidName", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
ctx := testutil.Context(t, testutil.WaitMedium)

o, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "something-unique",
DisplayName: "Something Unique",
})
require.NoError(t, err)

_, err = client.UpdateOrganization(ctx, o.ID.String(), codersdk.UpdateOrganizationRequest{
Name: "something! unique! but not! url! safe!",
})
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusBadRequest, apiErr.StatusCode())
})

t.Run("UpdateById", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
Expand Down
1 change: 1 addition & 0 deletions codersdk/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type OrganizationMember struct {
type CreateOrganizationRequest struct {
Name string `json:"name" validate:"required,username"`
DisplayName string `json:"display_name" validate:"required"`
Description string `json:"description"`
}

type UpdateOrganizationRequest struct {
Expand Down