Limit participants view of receiver-/sender messages and events.
[ecs.git] / test / models / message_test.rb
blob9a94c7779a94b7ec8524e46ee58e7ccc3d1aa9ed
1 # Copyright (C) 2007, 2008, 2009, 2010 Heiko Bernloehr (FreeIT.de).
2
3 # This file is part of ECS.
4
5 # ECS is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of
8 # the License, or (at your option) any later version.
9
10 # ECS is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Affero General Public License for more details.
14
15 # You should have received a copy of the GNU Affero General Public
16 # License along with ECS. If not, see <http://www.gnu.org/licenses/>.
19 require 'test_helper'
21 # request stub
22 class TestRequest
23   attr_accessor :headers, :raw_post
24   def initialize(h,rp); @headers, @raw_post = h, rp; end
25 end
27 class MessageTest < ActiveSupport::TestCase
28   test "named_scope__for_participant_receiver" do
29     m= Message.for_participant_receiver participants(:ilias_stgt)
30     assert_equal 2,m.length
31     assert m.include?(messages(:numlab_ex1))
32     assert m.include?(messages(:numlab_ulm_ex1))
33   end
35   # Change receivers community SUV (:ilias_stgt, :ilias_ulm) to :numlab_teacher
36   # and :ilias_ulm 
37   # Note: In the fixtures :ilias_stgt is configured as a receiver because it is
38   # in the SUV community. This is not correct because :ilias_stgt doesn't set
39   # community_selfrouting, but it's history.
40   test "update_receivers" do
41     headers={
42       "X-EcsReceiverMemberships" => "7,2",
43       "CONTENT_TYPE" => "text/plain"
44       }
45     raw_post= "hallo ich war da"
46     request= TestRequest.new(headers, raw_post)
47     assert_nothing_raised do
48       messages(:numlab_ex1).update__(request, "numlab", "exercises", participants(:ilias_stgt))
49     end
50     # :numlab_teacher is a new receiver and gets an created event
51     assert Participant.for_message(messages(:numlab_ex1)).uniq.include?(participants(:numlab_teacher))
52     assert Event.find_by_participant_id_and_ev_type_id(participants(:numlab_teacher).id,EvType.find_by_name("created"))
53     # :ilias_stgt isn't a receiver anymore and gets an destroyed event (:ilias_stgt was a receiver through fixture)
54     assert !Participant.for_message(messages(:numlab_ex1)).uniq.include?(participants(:ilias_stgt))
55     assert Event.find_by_participant_id_and_ev_type_id(participants(:ilias_stgt).id,EvType.find_by_name("destroyed"))
56     # :ilias_ulm is still a receiver and gets an updated event
57     assert Participant.for_message(messages(:numlab_ex1)).uniq.include?(participants(:ilias_ulm))
58     assert Event.find_by_participant_id_and_ev_type_id(participants(:ilias_ulm).id,EvType.find_by_name("updated"))
59     # number of receivers have to be two
60     assert_equal 2, Participant.for_message(messages(:numlab_ex1)).uniq.length
61   end  
63 # Auths
65   test "create_auths" do
66     headers={
67       "X-EcsReceiverMemberships" => "7,2",
68       "CONTENT_TYPE" => "application/json"
69       }
70     raw_post= Hash.new
71     raw_post[:realm]= <<-"HERE"
72     {
73       "realm":"#{Digest::SHA1.hexdigest 'https://ilias.uni-stuttgart.de/goto.php?target=crs_95034&client_id=USTGT'}"
74     }
75     HERE
76     raw_post[:url]= <<-'HERE'
77     {
78       "url":"https://ilias.uni-stuttgart.de/goto.php?target=crs_95034&client_id=USTGT"
79     }
80     HERE
81     raw_post.each do |k,v|
82       request= TestRequest.new(headers, v)
83       msg= nil
84       assert_nothing_raised do
85         msg= Message.create__(request, "sys", "auths", participants(:ilias_stgt))
86       end
87       assert Mime::Type.lookup(msg.content_type)== :json, "Content-Type is not application/json"
88       assert_equal participants(:ilias_stgt).id, msg.sender, "Unexpected creator of the message"
89       json= nil
90       assert_nothing_raised do
91         json= ActiveSupport::JSON.decode(msg.body)
92       end
93       assert json.keys.include?(k.to_s)
94       if k.to_s == "realm"
95         assert_equal Digest::SHA1.hexdigest("https://ilias.uni-stuttgart.de/goto.php?target=crs_95034&client_id=USTGT"), json[k.to_s]
96       end
97       if k.to_s == "url"
98         assert_equal "https://ilias.uni-stuttgart.de/goto.php?target=crs_95034&client_id=USTGT", json[k.to_s]
99       end
100       assert json.keys.include?("pid")
101       assert_equal participants(:ilias_stgt).id, json["pid"]
102     end
103   end
105   test "gc_outtimed_auths" do
106     auths_count= Auth.all.length
107     messages_count= Message.all.length
108     Auth.gc_outtimed
109     assert_equal messages_count-1, Message.all.length
110     assert_equal auths_count-1, Auth.all.length
111     assert_raise(ActiveRecord::RecordNotFound){auths(:outtimed)}
112   end