fixed Patch installation (bnc#579034), fixed possible race condition
[opensuse:yast-rest-service.git] / plugins / software / test / unit / repository_test.rb
1 #
2 # test 'Repository' model
3 #
4
5 require File.join(File.dirname(__FILE__), "..", "test_helper")
6 require File.join(File.dirname(__FILE__), "..", "packagekit_stub")
7
8 require 'repository'
9
10 # make the private method available for testing
11 class Repository
12   public :read_from_file
13 end
14
15
16 class RepositoryTest < ActiveSupport::TestCase
17
18   def setup
19     @pkit_stub = PackageKitStub.new
20     @transaction, @packagekit = PackageKit.connect
21
22     # Mock 'GetRepoList' by defining it
23     m = DBus::Method.new("GetRepoList")
24     m.from_prototype("in filter:s")
25     @transaction.methods[m.name] = m
26     
27     @transaction.stubs(:pkit_stub).returns(@pkit_stub)
28     class <<@transaction
29       def GetRepoList filter
30         rset = PackageKitResultSet.new "RepoDetail", :info => :s, :id => :s, :summary => :s
31         rset << ["factory-oss", "FACTORY-OSS", "true"]
32         rset << ["factory-non-oss", "FACTORY-NON-OSS", "false"]
33     
34         self.pkit_stub.result = rset
35       end
36
37       def RepoSetData id, key, value
38         return true
39       end
40
41       def RepoEnable id, value
42         return true
43       end
44
45     end
46
47     # don't read system *.repo files
48     Repository.any_instance.stubs(:read_file).returns(nil)
49   end
50
51   def test_repository_index
52     repos = Repository.find(:all)
53
54     assert_equal 2,repos.size
55     assert_equal "FACTORY-OSS", repos.first.name
56   end
57
58   def test_repository_details
59     repos = Repository.find('factory-oss')
60
61     assert_equal 1,repos.size
62     assert_equal "FACTORY-OSS", repos.first.name
63   end
64
65   def test_repository_create
66
67     assert_nothing_raised do
68       repo = Repository.new("factory-oss-new", "FACTORY-OSS-NEW", true)
69
70       repo.enabled = false
71       repo.autorefresh = false
72       repo.name = 'new name'
73       repo.url = 'http://test.com/repo'
74
75       repos = Repository.find(:all)
76       Repository.expects(:find).with(:all).returns(repos)
77
78       # ensure that 'add' is called here, not 'url' for updating an existing repo!
79       PackageKit.expects(:transact).with('RepoSetData', ["factory-oss-new", "add", repo.url])
80       # ensure that all properties are updated
81       PackageKit.expects(:transact).with('RepoEnable',  ["factory-oss-new", repo.enabled])
82       PackageKit.expects(:transact).with('RepoSetData', ["factory-oss-new", "prio", repo.priority.to_s])
83       PackageKit.expects(:transact).with('RepoSetData', ["factory-oss-new", "refresh", repo.autorefresh.to_s])
84       PackageKit.expects(:transact).with('RepoSetData', ["factory-oss-new", "keep", repo.keep_packages.to_s])
85       PackageKit.expects(:transact).with('RepoSetData', ["factory-oss-new", "name", repo.name])
86
87       assert repo.save
88     end
89   end
90
91   def test_repository_update
92
93     repo = Repository.find('factory-oss').first
94
95     repo.enabled = false
96     repo.autorefresh = false
97     repo.name = 'new name'
98     repo.url = 'ftp://new.url.com/repo'
99
100     repos = Repository.find(:all)
101     Repository.expects(:find).with(:all).returns(repos)
102
103     # ensure that 'url' is called here, not 'add' for adding a new repo!
104     PackageKit.expects(:transact).with('RepoSetData', ["factory-oss", "url", repo.url])
105     # ensure that all properties are updated
106     PackageKit.expects(:transact).with('RepoEnable',  ["factory-oss", repo.enabled])
107     PackageKit.expects(:transact).with('RepoSetData', ["factory-oss", "prio", repo.priority.to_s])
108     PackageKit.expects(:transact).with('RepoSetData', ["factory-oss", "refresh", repo.autorefresh.to_s])
109     PackageKit.expects(:transact).with('RepoSetData', ["factory-oss", "keep", repo.keep_packages.to_s])
110     PackageKit.expects(:transact).with('RepoSetData', ["factory-oss", "name", repo.name])
111
112     assert_nothing_raised do
113       assert repo.save
114     end
115   end
116
117   def test_repository_destroy
118     repo = Repository.find('factory-oss').first
119
120     PackageKit.expects(:transact).with('RepoSetData', ['factory-oss', 'remove', 'NONE']).returns(true)
121
122     assert_nothing_raised do
123       assert repo.destroy
124     end
125   end
126
127   # check to_xml producing correct (valid) output
128   def test_repository_to_xml
129     repo = Repository.new("factory-oss", "FACTORY-OSS", true)
130
131     xml = repo.to_xml
132     h = Hash.from_xml xml
133
134     assert_equal "factory-oss", h['repository']['id']
135   end
136
137   def test_repository_to_json
138     jsn = Repository.new("factory-oss", "FACTORY-OSS", true).to_json
139
140     repo_from_json = ActiveSupport::JSON.decode(jsn)
141
142     assert_equal repo_from_json, {"repository"=>{"name"=>"FACTORY-OSS", "autorefresh"=>true, "url"=>nil,
143         "priority"=>99, "id"=>"factory-oss", "enabled"=>true, "keep_packages"=>false}}
144   end
145
146   def test_repo_file_parsing
147     repo = Repository.new("factory-oss", "FACTORY-OSS", true)
148
149     fname = File.join(File.dirname(__FILE__), '..', 'fixtures', 'repos.d', 'Ruby.repo')
150     repo.read_from_file fname
151
152     # keep the alias, name and priority which was read from PackageKit
153     assert_equal 'FACTORY-OSS', repo.name
154     assert_equal 'factory-oss', repo.id
155     assert_equal true, repo.enabled
156
157     # check the read values
158     assert_equal 120, repo.priority
159     assert_equal false, repo.autorefresh
160     assert_equal 'http://download.opensuse.org/repositories/devel:/languages:/ruby:/extensions/openSUSE_Factory', repo.url
161
162     # use the default value if the key is missing
163     assert_equal false, repo.keep_packages
164   end
165 end