1 # Copyright (C) 2007, 2008, 2009, 2010 Heiko Bernloehr (FreeIT.de).
3 # This file is part of ECS.
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.
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.
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/>.
23 attr_accessor :headers, :raw_post
24 def initialize(h,rp); @headers, @raw_post = h, rp; 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))
35 # Change receivers community SUV (:ilias_stgt, :ilias_ulm) to :numlab_teacher
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
42 "X-EcsReceiverMemberships" => "7,2",
43 "CONTENT_TYPE" => "text/plain"
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))
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
65 test "create_auths" do
67 "X-EcsReceiverMemberships" => "7,2",
68 "CONTENT_TYPE" => "application/json"
71 raw_post[:realm]= <<-"HERE"
73 "realm":"#{Digest::SHA1.hexdigest 'https://ilias.uni-stuttgart.de/goto.php?target=crs_95034&client_id=USTGT'}"
76 raw_post[:url]= <<-'HERE'
78 "url":"https://ilias.uni-stuttgart.de/goto.php?target=crs_95034&client_id=USTGT"
81 raw_post.each do |k,v|
82 request= TestRequest.new(headers, v)
84 assert_nothing_raised do
85 msg= Message.create__(request, "sys", "auths", participants(:ilias_stgt))
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"
90 assert_nothing_raised do
91 json= ActiveSupport::JSON.decode(msg.body)
93 assert json.keys.include?(k.to_s)
95 assert_equal Digest::SHA1.hexdigest("https://ilias.uni-stuttgart.de/goto.php?target=crs_95034&client_id=USTGT"), json[k.to_s]
98 assert_equal "https://ilias.uni-stuttgart.de/goto.php?target=crs_95034&client_id=USTGT", json[k.to_s]
100 assert json.keys.include?("pid")
101 assert_equal participants(:ilias_stgt).id, json["pid"]
105 test "gc_outtimed_auths" do
106 auths_count= Auth.all.length
107 messages_count= Message.all.length
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)}