Skip to content

Commit

Permalink
Merge pull request concerto#902 from concerto/test_coverage_models
Browse files Browse the repository at this point in the history
additional installment of model tests
  • Loading branch information
mfrederickson committed Mar 8, 2014
2 parents c81e9eb + a938c57 commit 151eed6
Show file tree
Hide file tree
Showing 22 changed files with 546 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/models/content.rb
Expand Up @@ -66,7 +66,7 @@ def is_active?

# Determine if content is expired based on its end time.
def is_expired?
(end_time < Clock.time)
(!end_time.nil? and end_time < Clock.time)
end

def is_orphan?
Expand Down
2 changes: 1 addition & 1 deletion app/models/group.rb
Expand Up @@ -37,7 +37,7 @@ def update_membership_perms
end

def create_leader
self.new_leader = Membership.create(:user_id => new_leader, :group_id => self.id, :level => 9) if new_leader.present?
self.new_leader = Membership.create(:user_id => new_leader, :group_id => self.id, :level => Membership::LEVELS[:leader]) if new_leader.present?
end

# Deliver a list of only users not currently in the group
Expand Down
6 changes: 3 additions & 3 deletions app/models/submission.rb
Expand Up @@ -78,9 +78,9 @@ def self.deny_old_expired
Submission.pending.expired.readonly(false).each do |submission|
submission.moderation_flag = false
submission.moderation_reason = 'Content expired before moderation could occur'
Rails.logger.info submission
print submission.to_yaml
print submission.errors.to_yaml
#Rails.logger.info submission
#print submission.to_yaml
#print submission.errors.to_yaml
submission.save
end
end
Expand Down
35 changes: 20 additions & 15 deletions app/models/template.rb
Expand Up @@ -142,8 +142,7 @@ def import_archive(archive)
return false
end

file = archive.tempfile unless archive.is_a? Rack::Test::UploadedFile
file ||= archive
file = archive.path

require 'zip/zip'
zip_file = Zip::ZipFile.open(file)
Expand Down Expand Up @@ -173,20 +172,26 @@ def import_archive(archive)
return false
end

if import_xml(xml_data)
self.media.build({:key=>"original", :file_name => image_file.name,
:file_type => MIME::Types.type_for(image_file.name).first.content_type})
self.media.first.file_size = image_file.size
self.media.first.file_data = image_file.get_input_stream.read

if !css_file.nil?
m = self.media.build({:key=>"css", :file_name => css_file.name,
:file_type => MIME::Types.type_for(css_file.name).first.content_type})
m.file_size = css_file.size
m.file_data = css_file.get_input_stream.read
begin
if import_xml(xml_data)
self.media.build({:key=>"original", :file_name => image_file.name,
:file_type => MIME::Types.type_for(image_file.name).first.content_type})
self.media.first.file_size = image_file.size
self.media.first.file_data = image_file.get_input_stream.read

if !css_file.nil?
m = self.media.build({:key=>"css", :file_name => css_file.name,
:file_type => MIME::Types.type_for(css_file.name).first.content_type})
m.file_size = css_file.size
m.file_data = css_file.get_input_stream.read
end
return true
else
self.errors.add(:base, I18n.t('templates.new.invalid_xml'))
return false
end
return true
else
rescue REXML::ParseException => e
Rails.logger.error("invalid xml when importing template package - #{e.message}")
self.errors.add(:base, I18n.t('templates.new.invalid_xml'))
return false
end
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/contents.yml
Expand Up @@ -112,4 +112,4 @@ sample_image:
end_time: 2100-02-19 21:13:35
user: katie
kind: graphic
type: Graphic
type: Graphic
Binary file added test/fixtures/files/ArchiveWithEmptyXml.zip
Binary file not shown.
Binary file added test/fixtures/files/ArchiveWithInvalidXml.zip
Binary file not shown.
Binary file added test/fixtures/files/ArchiveWithoutImage.zip
Binary file not shown.
Binary file added test/fixtures/files/ArchiveWithoutXml.zip
Binary file not shown.
16 changes: 16 additions & 0 deletions test/fixtures/files/empty.xml
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<template>
<name></name>
<width></width>
<height></height>
<author></author>
<field>
<name></name>
<type></type>
<style></style>
<left></left>
<top></top>
<width></width>
<height></height>
</field>
</template>
17 changes: 17 additions & 0 deletions test/fixtures/files/invalid.xml
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<template>
<name>Sample Template</name>
<width>1920 MISSING END TAG
<height>1080</height>
<author>Testing Author</author>
<hidden>false</hidden>
<field>
<name>Graphics</name>
<type>Graphics</type>
<style>border:solid 2px #ccc;</style>
<left>0.025</left>
<top>0.026</top>
<width>0.567</width>
<height>0.77</height>
MISSING END TAG
</template>
4 changes: 4 additions & 0 deletions test/fixtures/groups.yml
Expand Up @@ -5,3 +5,7 @@ wtg:

rpitv:
name: RPITV

unused:
name: abandoned

10 changes: 10 additions & 0 deletions test/fixtures/memberships.yml
Expand Up @@ -15,3 +15,13 @@ karen_wtg:
group: wtg
level: <%= Membership::LEVELS[:regular] %>
permissions: 0

kristen_rpitv:
user: kristen
group: rpitv
level: <%= Membership::LEVELS[:denied] %>

kristen_unused:
user: kristen
group: unused
level: <%= Membership::LEVELS[:pending] %>
2 changes: 1 addition & 1 deletion test/functional/memberships_controller_test.rb
Expand Up @@ -10,7 +10,7 @@ def setup
test "should create pending membership" do
sign_in users(:kristen)
assert_difference('Membership.count', 1) do
post :create, {:membership => {:user_id => users(:kristen).id}, :group_id => groups(:rpitv).id}
post :create, {:membership => {:user_id => users(:kristen).id}, :group_id => groups(:wtg).id}
end
actual = assigns(:membership)
group = assigns(:group)
Expand Down
117 changes: 117 additions & 0 deletions test/unit/content_test.rb
Expand Up @@ -123,4 +123,121 @@ class TestContent < DynamicContent
c = Content.new()
assert_equal nil, c.perform_action(:save, {:current_user => users(:katie)})
end

test "is_orphan? identifies content without submissions" do
c = Content.new(:name => "Sample Ticker",
:kind_id => kinds(:ticker).id,
:duration => 10,
:user => users(:katie))
assert c.save
assert c.is_orphan?
end

test "is_denied? detects if content denied on any feed" do
c = Content.new(:name => "TickerDeniedOnOne",
:kind_id => kinds(:ticker).id,
:duration => 10,
:user => users(:katie),
:start_time => 2.days.ago,
:end_time => Time.now.tomorrow)
assert c.save

Submission.create({:content => c, :duration => 5, :feed => feeds(:announcements)})
assert !c.is_denied?
assert Submission.create({
:content => c,
:duration => 5,
:feed => feeds(:boring_announcements),
:moderator => users(:admin),
:moderation_flag => true})
assert !c.is_denied?
assert Submission.create({
:content => c,
:duration => 5,
:feed => feeds(:important_announcements),
:moderator => users(:admin),
:moderation_flag => false})
assert c.is_denied?
end

test "is_pending? detects if content pending on any feed" do
c = Content.new(:name => "TickerPendingOnOne",
:kind_id => kinds(:ticker).id,
:duration => 10,
:user => users(:katie),
:start_time => 2.days.ago,
:end_time => Time.now.tomorrow)
assert c.save

assert Submission.create({
:content => c,
:duration => 5,
:feed => feeds(:boring_announcements),
:moderator => users(:admin),
:moderation_flag => true})
assert !c.is_pending?
assert Submission.create({
:content => c,
:duration => 5,
:feed => feeds(:important_announcements),
:moderator => users(:admin),
:moderation_flag => false})
assert !c.is_pending?
Submission.create({:content => c, :duration => 5, :feed => feeds(:announcements)})
assert c.is_pending?
end

test "is_approved? true only when content is approved on all feeds" do
c = Content.new(:name => "TickerApprovedOnAll",
:kind_id => kinds(:ticker).id,
:duration => 10,
:user => users(:katie),
:start_time => 2.days.ago,
:end_time => Time.now.tomorrow)
assert c.save

assert Submission.create({
:content => c,
:duration => 5,
:feed => feeds(:boring_announcements),
:moderator => users(:admin),
:moderation_flag => true})
assert c.is_approved?
sub = Submission.create({
:content => c,
:duration => 5,
:feed => feeds(:important_announcements),
:moderator => users(:admin),
:moderation_flag => false})
assert !c.is_approved?
sub.moderation_flag = true
sub.save
assert c.is_approved?
Submission.create({:content => c, :duration => 5, :feed => feeds(:announcements)})
assert !c.is_approved?
end

test "base preview is empty" do
assert_equal "", Content::preview
end

test "filter content by screen" do
assert_equal 1, Content::filter_all_content({ :screen => screens(:one) }).count
end

test "filter content by feed" do
assert_equal 2, Content::filter_all_content({ :feed => feeds(:service) }).count
end

test "filter content by user" do
assert_equal 10, Content::filter_all_content({ :user => users(:katie) }).count
end

test "filter content by type" do
assert_equal 9, Content::filter_all_content({ :type => 'Ticker' }).count
end

test "filter content by nothing returns all" do
assert_equal 11, Content::filter_all_content({}).count
end
end

0 comments on commit 151eed6

Please sign in to comment.