Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
f707bf072623
Commit
32582045
authored
Nov 14, 2017
by
Michael Kozono
Browse files
Refactor specs
parent
9f04f64a0cbd
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
spec/lib/gitlab/background_migration/populate_untracked_uploads_spec.rb
View file @
f707bf07
This diff is collapsed.
Click to expand it.
spec/lib/gitlab/background_migration/prepare_untracked_uploads_spec.rb
View file @
f707bf07
require
'spec_helper'
describe
Gitlab
::
BackgroundMigration
::
PrepareUntrackedUploads
,
:migration
,
:sidekiq
,
schema:
20171103140253
do
let!
(
:untracked_files_for_uploads
)
{
table
(
:untracked_files_for_uploads
)
}
include
TrackUntrackedUploadsHelpers
let
(
:user1
)
{
create
(
:user
)
}
let
(
:user2
)
{
create
(
:user
)
}
let
(
:project1
)
{
create
(
:project
)
}
let
(
:project2
)
{
create
(
:project
)
}
let
(
:appearance
)
{
create
(
:appearance
)
}
let!
(
:untracked_files_for_uploads
)
{
table
(
:untracked_files_for_uploads
)
}
matcher
:be_scheduled_migration
do
|*
expected
|
match
do
|
migration
|
...
...
@@ -22,20 +18,18 @@
end
context
'when files were uploaded before and after hashed storage was enabled'
do
before
do
fixture
=
Rails
.
root
.
join
(
'spec'
,
'fixtures'
,
'rails_sample.jpg'
)
uploaded_file
=
fixture_file_upload
(
fixture
)
let!
(
:appearance
)
{
create
(
:appearance
,
logo:
uploaded_file
,
header_logo:
uploaded_file
)
}
let!
(
:user
)
{
create
(
:user
,
:with_avatar
)
}
let!
(
:project1
)
{
create
(
:project
,
:with_avatar
)
}
let
(
:project2
)
{
create
(
:project
)
}
# instantiate after enabling hashed_storage
user1
.
update
(
avatar:
uploaded_file
)
project1
.
update
(
avatar:
uploaded_file
)
appearance
.
update
(
logo:
uploaded_file
,
header_logo:
uploaded_file
)
uploaded_file
=
fixture_file_upload
(
fixture
)
UploadService
.
new
(
project1
,
uploaded_file
,
FileUploader
).
execute
# Markdown upload
before
do
# Markdown upload before enabling hashed_storage
UploadService
.
new
(
project1
,
uploaded_file
,
FileUploader
).
execute
stub_application_setting
(
hashed_storage_enabled:
true
)
# Hashed files
uploaded_file
=
fixture_file_upload
(
fixture
)
# Markdown upload after enabling hashed_storage
UploadService
.
new
(
project2
,
uploaded_file
,
FileUploader
).
execute
end
...
...
spec/migrations/track_untracked_uploads_spec.rb
View file @
f707bf07
...
...
@@ -4,9 +4,7 @@
describe
TrackUntrackedUploads
,
:migration
,
:sidekiq
do
include
TrackUntrackedUploadsHelpers
class
UntrackedFile
<
ActiveRecord
::
Base
self
.
table_name
=
'untracked_files_for_uploads'
end
let
(
:untracked_files_for_uploads
)
{
table
(
:untracked_files_for_uploads
)
}
matcher
:be_scheduled_migration
do
match
do
|
migration
|
...
...
@@ -46,39 +44,36 @@ class UntrackedFile < ActiveRecord::Base
component
# filename
].
flatten
.
join
(
'/'
)
record
=
U
ntracked
F
ile
.
create!
(
path:
long_path
)
record
=
u
ntracked
_f
ile
s_for_uploads
.
create!
(
path:
long_path
)
expect
(
record
.
reload
.
path
.
size
).
to
eq
(
519
)
end
context
'with tracked and untracked uploads'
do
let
(
:
user1
)
{
create
(
:
user
)
}
let
(
:user
2
)
{
create
(
:user
)
}
let
(
:
project1
)
{
create
(
:
project
)
}
let
(
:project
2
)
{
create
(
:project
)
}
let
(
:
appearance
)
{
create
(
:
appearance
)
}
let
!
(
:
appearance
)
{
create
(
:
appearance
,
logo:
uploaded_file
,
header_logo:
uploaded_file
)
}
let
!
(
:user
1
)
{
create
(
:user
,
:with_avatar
)
}
let
!
(
:
user2
)
{
create
(
:
user
,
:with_avatar
)
}
let
!
(
:project
1
)
{
create
(
:project
,
:with_avatar
)
}
let
!
(
:
project2
)
{
create
(
:
project
,
:with_avatar
)
}
let
(
:uploads
)
{
table
(
:uploads
)
}
before
do
fixture
=
Rails
.
root
.
join
(
'spec'
,
'fixtures'
,
'rails_sample.jpg'
)
# Tracked, by doing normal file upload
uploaded_file
=
fixture_file_upload
(
fixture
)
user1
.
update
(
avatar:
uploaded_file
)
project1
.
update
(
avatar:
uploaded_file
)
upload_result
=
UploadService
.
new
(
project1
,
uploaded_file
,
FileUploader
).
execute
# Markdown upload
@project1_markdown_upload_path
=
upload_result
[
:url
].
sub
(
%r{
\A
/uploads/}
,
''
)
appearance
.
update
(
logo:
uploaded_file
)
# Untracked, by doing normal file upload then deleting records from DB
uploaded_file
=
fixture_file_upload
(
fixture
)
user2
.
update
(
avatar:
uploaded_file
)
UploadService
.
new
(
project1
,
uploaded_file
,
FileUploader
).
execute
# Markdown upload
UploadService
.
new
(
project2
,
uploaded_file
,
FileUploader
).
execute
# Markdown upload
# Save expected Upload attributes
@appearance_logo_attributes
=
appearance
.
uploads
.
where
(
"path like '%/logo/%'"
).
first
.
attributes
.
slice
(
'path'
,
'uploader'
,
'size'
,
'checksum'
)
@appearance_header_logo_attributes
=
appearance
.
uploads
.
where
(
"path like '%/header_logo/%'"
).
first
.
attributes
.
slice
(
'path'
,
'uploader'
,
'size'
,
'checksum'
)
@user1_avatar_attributes
=
user1
.
uploads
.
first
.
attributes
.
slice
(
'path'
,
'uploader'
,
'size'
,
'checksum'
)
@user2_avatar_attributes
=
user2
.
uploads
.
first
.
attributes
.
slice
(
'path'
,
'uploader'
,
'size'
,
'checksum'
)
@project1_avatar_attributes
=
project1
.
uploads
.
first
.
attributes
.
slice
(
'path'
,
'uploader'
,
'size'
,
'checksum'
)
@project2_avatar_attributes
=
project2
.
uploads
.
first
.
attributes
.
slice
(
'path'
,
'uploader'
,
'size'
,
'checksum'
)
@project1_markdown_attributes
=
project1
.
uploads
.
last
.
attributes
.
slice
(
'path'
,
'uploader'
,
'size'
,
'checksum'
)
@project2_markdown_attributes
=
project2
.
uploads
.
last
.
attributes
.
slice
(
'path'
,
'uploader'
,
'size'
,
'checksum'
)
# Untrack 4 files
user2
.
uploads
.
delete_all
project2
.
update
(
avatar:
uploaded_file
)
upload_result
=
UploadService
.
new
(
project2
,
uploaded_file
,
FileUploader
).
execute
# Markdown upload
@project2_markdown_upload_path
=
upload_result
[
:url
].
sub
(
%r{
\A
/uploads/}
,
''
)
project2
.
uploads
.
delete_all
appearance
.
update
(
header_logo:
uploaded_file
)
appearance
.
uploads
.
last
.
destroy
project2
.
uploads
.
delete_all
# 2 files: avatar and a Markdown upload
appearance
.
uploads
.
where
(
"path like '%header_logo%'"
).
delete_all
end
it
'tracks untracked uploads'
do
...
...
@@ -87,23 +82,10 @@ class UntrackedFile < ActiveRecord::Base
migrate!
end
.
to
change
{
uploads
.
count
}.
from
(
4
).
to
(
8
)
expect
(
user2
.
reload
.
uploads
.
first
.
attributes
).
to
include
({
"path"
=>
"uploads/-/system/user/avatar/
#{
user2
.
id
}
/rails_sample.jpg"
,
"uploader"
=>
"AvatarUploader"
}.
merge
(
rails_sample_jpg_attrs
))
expect
(
project2
.
reload
.
uploads
.
first
.
attributes
).
to
include
({
"path"
=>
"uploads/-/system/project/avatar/
#{
project2
.
id
}
/rails_sample.jpg"
,
"uploader"
=>
"AvatarUploader"
}.
merge
(
rails_sample_jpg_attrs
))
expect
(
appearance
.
reload
.
uploads
.
count
).
to
eq
(
2
)
expect
(
appearance
.
uploads
.
last
.
attributes
).
to
include
({
"path"
=>
"uploads/-/system/appearance/header_logo/
#{
appearance
.
id
}
/rails_sample.jpg"
,
"uploader"
=>
"AttachmentUploader"
}.
merge
(
rails_sample_jpg_attrs
))
expect
(
project2
.
uploads
.
last
.
attributes
).
to
include
({
"path"
=>
@project2_markdown_upload_path
,
"uploader"
=>
"FileUploader"
}.
merge
(
rails_sample_jpg_attrs
))
expect
(
appearance
.
reload
.
uploads
.
where
(
"path like '%/header_logo/%'"
).
first
.
attributes
).
to
include
(
@appearance_header_logo_attributes
)
expect
(
user2
.
reload
.
uploads
.
first
.
attributes
).
to
include
(
@user2_avatar_attributes
)
expect
(
project2
.
reload
.
uploads
.
first
.
attributes
).
to
include
(
@project2_avatar_attributes
)
expect
(
project2
.
uploads
.
last
.
attributes
).
to
include
(
@project2_markdown_attributes
)
end
end
...
...
@@ -111,31 +93,19 @@ class UntrackedFile < ActiveRecord::Base
Sidekiq
::
Testing
.
inline!
do
migrate!
expect
(
user1
.
reload
.
uploads
.
first
.
attributes
).
to
include
({
"path"
=>
"uploads/-/system/user/avatar/
#{
user1
.
id
}
/rails_sample.jpg"
,
"uploader"
=>
"AvatarUploader"
}.
merge
(
rails_sample_jpg_attrs
))
expect
(
project1
.
reload
.
uploads
.
first
.
attributes
).
to
include
({
"path"
=>
"uploads/-/system/project/avatar/
#{
project1
.
id
}
/rails_sample.jpg"
,
"uploader"
=>
"AvatarUploader"
}.
merge
(
rails_sample_jpg_attrs
))
expect
(
appearance
.
reload
.
uploads
.
first
.
attributes
).
to
include
({
"path"
=>
"uploads/-/system/appearance/logo/
#{
appearance
.
id
}
/rails_sample.jpg"
,
"uploader"
=>
"AttachmentUploader"
}.
merge
(
rails_sample_jpg_attrs
))
expect
(
project1
.
uploads
.
last
.
attributes
).
to
include
({
"path"
=>
@project1_markdown_upload_path
,
"uploader"
=>
"FileUploader"
}.
merge
(
rails_sample_jpg_attrs
))
expect
(
appearance
.
reload
.
uploads
.
where
(
"path like '%/logo/%'"
).
first
.
attributes
).
to
include
(
@appearance_logo_attributes
)
expect
(
user1
.
reload
.
uploads
.
first
.
attributes
).
to
include
(
@user1_avatar_attributes
)
expect
(
project1
.
reload
.
uploads
.
first
.
attributes
).
to
include
(
@project1_avatar_attributes
)
expect
(
project1
.
uploads
.
last
.
attributes
).
to
include
(
@project1_markdown_attributes
)
end
end
it
'all
U
ntracked
F
ile records are marked as tracked'
do
it
'all
u
ntracked
_f
ile
s_for_uploads
records are marked as tracked'
do
Sidekiq
::
Testing
.
inline!
do
migrate!
expect
(
U
ntracked
F
ile
.
count
).
to
eq
(
8
)
expect
(
U
ntracked
F
ile
.
count
).
to
eq
(
U
ntracked
F
ile
.
where
(
tracked:
true
).
count
)
expect
(
u
ntracked
_f
ile
s_for_uploads
.
count
).
to
eq
(
8
)
expect
(
u
ntracked
_f
ile
s_for_uploads
.
count
).
to
eq
(
u
ntracked
_f
ile
s_for_uploads
.
where
(
tracked:
true
).
count
)
end
end
end
...
...
spec/support/track_untracked_uploads_helpers.rb
View file @
f707bf07
module
TrackUntrackedUploadsHelpers
def
rails_sample_jpg_attrs
@rails_sample_jpg_attrs
||=
{
"size"
=>
File
.
size
(
rails_sample_file_path
),
"checksum"
=>
Digest
::
SHA256
.
file
(
rails_sample_file_path
).
hexdigest
}
end
def
rails_sample_file_path
Rails
.
root
.
join
(
'spec'
,
'fixtures'
,
'rails_sample.jpg'
)
def
uploaded_file
fixture_path
=
Rails
.
root
.
join
(
'spec'
,
'fixtures'
,
'rails_sample.jpg'
)
fixture_file_upload
(
fixture_path
)
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment