-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathfile_spec.rb
More file actions
154 lines (120 loc) · 4.41 KB
/
file_spec.rb
File metadata and controls
154 lines (120 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe CodeOcean::File do
let(:file) { described_class.create.tap {|file| file.update(content: nil, hidden: nil, read_only: nil) } }
it 'validates the presence of a file type' do
expect(file.errors[:file_type]).to be_present
end
it 'validates the presence of the hidden flag' do
expect(file.errors[:hidden]).to be_present
file.update(hidden: false)
expect(file.errors[:hidden]).to be_blank
end
it 'validates the presence of a name' do
expect(file.errors[:name]).to be_present
end
it 'validates the presence of the read-only flag' do
expect(file.errors[:read_only]).to be_present
file.update(read_only: false)
expect(file.errors[:read_only]).to be_blank
end
context 'with a teacher-defined test' do
before { file.update(role: 'teacher_defined_test') }
it 'validates the presence of a feedback message' do
expect(file.errors[:feedback_message]).to be_present
end
it 'validates the numericality of a weight' do
file.update(weight: 'heavy')
expect(file.errors[:weight]).to be_present
end
it 'validates the presence of a weight' do
expect(file.errors[:weight]).to be_present
end
end
context 'with another file type' do
before { file.update(role: 'regular_file') }
it 'validates the absence of a feedback message' do
file.update(feedback_message: 'Your solution is not correct yet.')
expect(file.errors[:feedback_message]).to be_present
end
it 'validates the absence of a weight' do
allow(file).to receive(:clear_weight)
file.update(weight: 1)
expect(file.errors[:weight]).to be_present
end
end
context 'with xml_id_path' do
let(:exercise) { create(:dummy) }
let(:file) { build(:file, context: file_context, xml_id_path: xml_id_path) }
let(:file_context) { exercise }
let(:xml_id_path) { ['abcde'] }
before do
create(:file, context: exercise, xml_id_path: ['abcde'])
file.validate
end
it 'has an error for xml_id_path' do
expect(file.errors[:xml_id_path]).to be_present
end
context 'when second file has a different exercise' do
let(:file_context) { create(:dummy) }
it 'has no error for xml_id_path' do
expect(file.errors[:xml_id_path]).not_to be_present
end
end
context 'when second file has a different xml_id_path' do
let(:xml_id_path) { ['foobar'] }
it 'has no error for xml_id_path' do
expect(file.errors[:xml_id_path]).not_to be_present
end
end
context 'when file_context is not Exercise' do
let(:file_context) { create(:submission) }
it 'has an error for xml_id_path' do
expect(file.errors[:xml_id_path]).to be_present
end
end
end
context 'with a native file' do
let(:file) { create(:file, :image) }
after { file.native_file.remove! }
context 'when the path has not been modified' do
it 'reads the native file' do
expect(file.read).to be_present
end
end
context 'when the path has been modified' do
before do
file.update_column(:native_file, '../../../../database.yml') # rubocop:disable Rails/SkipsModelValidations
file.reload
end
it 'does not read the native file' do
expect(file.read).not_to be_present
end
end
context 'when a symlink is used' do
let(:fake_upload_location) { File.join(CarrierWave::Uploader::Base.new.root, 'uploads', 'files', 'database.yml') }
before do
FileUtils.mkdir_p(File.dirname(fake_upload_location))
FileUtils.touch Rails.root.join('config/database.yml')
File.symlink Rails.root.join('config/database.yml'), fake_upload_location
file.update_column(:native_file, '../database.yml') # rubocop:disable Rails/SkipsModelValidations
file.reload
end
after { File.delete(fake_upload_location) }
it 'does not read the native file' do
expect(file.read).not_to be_present
end
end
end
context 'when a file with same attributes (path, name file_type context_id context_type role) already exists' do
before do
create(:file, name: 'static', context: exercise)
file.validate
end
let(:exercise) { create(:dummy) }
let(:file) { build(:file, name: 'static', context: exercise) }
it 'has an error for path' do
expect(file.errors[:path]).to be_present
end
end
end