Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
eb2f2a05
Commit
eb2f2a05
authored
8 years ago
by
Cam Saül
Browse files
Options
Downloads
Patches
Plain Diff
Make sure collection names can't be blank; add tests
parent
98f9cf76
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/models/collection.clj
+12
-4
12 additions, 4 deletions
src/metabase/models/collection.clj
test/metabase/models/collection_test.clj
+13
-1
13 additions, 1 deletion
test/metabase/models/collection_test.clj
with
25 additions
and
5 deletions
src/metabase/models/collection.clj
+
12
−
4
View file @
eb2f2a05
(
ns
metabase.models.collection
(
:require
[
clojure.data
:as
data
]
(
:require
(
clojure
[
data
:as
data
]
[
string
:as
str
])
[
schema.core
:as
s
]
[
metabase.api.common
:refer
[
*current-user-id*
]]
[
metabase.db
:as
db
]
...
...
@@ -28,11 +29,18 @@
(
when
(
or
(
not
(
string?
hex-color
))
(
not
(
re-matches
hex-color-regex
hex-color
)))
(
throw
(
ex-info
"Invalid color"
{
:status-code
400
,
:erros
{
:color
"must be a valid 6-character hex color code"
}}))))
{
:status-code
400
,
:errors
{
:color
"must be a valid 6-character hex color code"
}}))))
(
defn-
slugify
[
collection-name
]
;; double-check that someone isn't trying to use a blank string as the collection name
(
when
(
str/blank?
collection-name
)
(
throw
(
ex-info
"Collection name cannot be blank!"
{
:status-code
400
,
:errors
{
:name
"cannot be blank"
}})))
(
u/slugify
collection-name
collection-slug-max-length
))
(
defn-
pre-insert
[{
collection-name
:name,
color
:color,
:as
collection
}]
(
assert-valid-hex-color
color
)
(
assoc
collection
:slug
(
u/prog1
(
u/
slugify
collection-name
collection-slug-max-length
)
(
assoc
collection
:slug
(
u/prog1
(
slugify
collection-name
)
(
assert-unique-slug
<>
))))
(
defn-
pre-update
[{
collection-name
:name,
id
:id,
color
:color,
archived?
:archived,
:as
collection
}]
...
...
@@ -45,7 +53,7 @@
;; slugify the collection name and make sure it's unique
(
if-not
collection-name
collection
(
assoc
collection
:slug
(
u/prog1
(
u/
slugify
collection-name
collection-slug-max-length
)
(
assoc
collection
:slug
(
u/prog1
(
slugify
collection-name
)
(
or
(
db/exists?
Collection,
:slug
<>,
:id
id
)
; if slug hasn't changed no need to check for uniqueness
(
assert-unique-slug
<>
))))))
; otherwise check to make sure the new slug is unique
...
...
This diff is collapsed.
Click to expand it.
test/metabase/models/collection_test.clj
+
13
−
1
View file @
eb2f2a05
...
...
@@ -43,7 +43,6 @@
Collection
[
_
{
:name
"my_favorite Cards"
}]]
:ok
))
;; check that archiving a collection archives its cards as well
(
expect
true
...
...
@@ -61,3 +60,16 @@
(
db/update!
Collection
(
u/get-id
collection
)
:archived
false
)
(
db/select-one-field
:archived
Card
:id
(
u/get-id
card
))))
;; check that collections' names cannot be blank
(
expect
Exception
(
tu/with-temp
Collection
[
collection
{
:name
""
}]
collection
))
;; check we can't change the name of a Collection to a blank string
(
expect
Exception
(
tu/with-temp
Collection
[
collection
]
(
db/update!
Collection
(
u/get-id
collection
)
:name
""
)))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment