Skip to content

Commit

Permalink
Add anchors to name regex
Browse files Browse the repository at this point in the history
apparently validate_format doesn't default to whole-string matches, regex anchors solve for that
  • Loading branch information
michaeljguarino committed Oct 27, 2024
1 parent 20083a1 commit 71b1d63
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions apps/core/lib/core/schema/console_instance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ defmodule Core.Schema.ConsoleInstance do
|> cast(attrs, @valid)
|> cast_embed(:configuration, with: &configuration_changeset/2)
|> cast_embed(:instance_status, with: &status_changeset/2)
|> validate_required(@valid -- ~w(external_id postgres_id cluster_id)a)
|> validate_required(@valid -- ~w(external_id name postgres_id cluster_id)a)
|> foreign_key_constraint(:cluster_id)
|> foreign_key_constraint(:postgres_id)
|> foreign_key_constraint(:owner_id)
|> unique_constraint(:subdomain)
|> unique_constraint(:name)
|> validate_format(:name, ~r/[a-z][a-z0-9]{5,10}/, message: "must be an alphanumeric string between 5 and 11 characters")
|> validate_format(:name, ~r/^[a-z][a-z0-9]{5,10}$/, message: "must be an alphanumeric string between 5 and 11 characters")
|> validate_region()
end

Expand All @@ -133,7 +133,7 @@ defmodule Core.Schema.ConsoleInstance do
|> foreign_key_constraint(:owner_id)
|> unique_constraint(:subdomain)
|> unique_constraint(:name)
|> validate_format(:name, ~r/[a-z][a-z0-9]{5,10}/, message: "must be an alphanumeric string between 5 and 11 characters")
|> validate_format(:name, ~r/^[a-z][a-z0-9]{5,10}$/, message: "must be an alphanumeric string between 5 and 11 characters")
|> validate_region()
end

Expand Down
15 changes: 15 additions & 0 deletions apps/core/test/services/cloud_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ defmodule Core.Services.CloudTest do
assert_receive {:event, %PubSub.ConsoleInstanceCreated{item: ^instance}}
end

test "cannot create instances w/ capitalized names" do
account = insert(:account)
enterprise_plan(account)
user = admin_user(account)
insert(:repository, name: "console")

{:error, _} = Cloud.create_instance(%{
type: :dedicated,
name: "My-Cluster",
cloud: :aws,
region: "us-east-1",
size: :small
}, user)
end

test "nonenterprise plans cannot create a dedicated cloud console instance" do
account = insert(:account)
enable_features(account, [:cd])
Expand Down

0 comments on commit 71b1d63

Please sign in to comment.