##// END OF EJS Templates
Diff style (inline or side by side) automatically saved as a user preference....
Jean-Philippe Lang -
r880:1454a711caf6
parent child
Show More
@@ -1,257 +1,264
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'SVG/Graph/Bar'
18 require 'SVG/Graph/Bar'
19 require 'SVG/Graph/BarHorizontal'
19 require 'SVG/Graph/BarHorizontal'
20 require 'digest/sha1'
20 require 'digest/sha1'
21
21
22 class RepositoriesController < ApplicationController
22 class RepositoriesController < ApplicationController
23 layout 'base'
23 layout 'base'
24 before_filter :find_repository, :except => :edit
24 before_filter :find_repository, :except => :edit
25 before_filter :find_project, :only => :edit
25 before_filter :find_project, :only => :edit
26 before_filter :authorize
26 before_filter :authorize
27 accept_key_auth :revisions
27 accept_key_auth :revisions
28
28
29 def edit
29 def edit
30 @repository = @project.repository
30 @repository = @project.repository
31 if !@repository
31 if !@repository
32 @repository = Repository.factory(params[:repository_scm])
32 @repository = Repository.factory(params[:repository_scm])
33 @repository.project = @project
33 @repository.project = @project
34 end
34 end
35 if request.post?
35 if request.post?
36 @repository.attributes = params[:repository]
36 @repository.attributes = params[:repository]
37 @repository.save
37 @repository.save
38 end
38 end
39 render(:update) {|page| page.replace_html "tab-content-repository", :partial => 'projects/settings/repository'}
39 render(:update) {|page| page.replace_html "tab-content-repository", :partial => 'projects/settings/repository'}
40 end
40 end
41
41
42 def destroy
42 def destroy
43 @repository.destroy
43 @repository.destroy
44 redirect_to :controller => 'projects', :action => 'settings', :id => @project, :tab => 'repository'
44 redirect_to :controller => 'projects', :action => 'settings', :id => @project, :tab => 'repository'
45 end
45 end
46
46
47 def show
47 def show
48 # check if new revisions have been committed in the repository
48 # check if new revisions have been committed in the repository
49 @repository.fetch_changesets if Setting.autofetch_changesets?
49 @repository.fetch_changesets if Setting.autofetch_changesets?
50 # get entries for the browse frame
50 # get entries for the browse frame
51 @entries = @repository.entries('')
51 @entries = @repository.entries('')
52 # latest changesets
52 # latest changesets
53 @changesets = @repository.changesets.find(:all, :limit => 10, :order => "committed_on DESC")
53 @changesets = @repository.changesets.find(:all, :limit => 10, :order => "committed_on DESC")
54 show_error and return unless @entries || @changesets.any?
54 show_error and return unless @entries || @changesets.any?
55 end
55 end
56
56
57 def browse
57 def browse
58 @entries = @repository.entries(@path, @rev)
58 @entries = @repository.entries(@path, @rev)
59 if request.xhr?
59 if request.xhr?
60 @entries ? render(:partial => 'dir_list_content') : render(:nothing => true)
60 @entries ? render(:partial => 'dir_list_content') : render(:nothing => true)
61 else
61 else
62 show_error unless @entries
62 show_error unless @entries
63 end
63 end
64 end
64 end
65
65
66 def changes
66 def changes
67 @entry = @repository.scm.entry(@path, @rev)
67 @entry = @repository.scm.entry(@path, @rev)
68 show_error and return unless @entry
68 show_error and return unless @entry
69 @changesets = @repository.changesets_for_path(@path)
69 @changesets = @repository.changesets_for_path(@path)
70 end
70 end
71
71
72 def revisions
72 def revisions
73 @changeset_count = @repository.changesets.count
73 @changeset_count = @repository.changesets.count
74 @changeset_pages = Paginator.new self, @changeset_count,
74 @changeset_pages = Paginator.new self, @changeset_count,
75 25,
75 25,
76 params['page']
76 params['page']
77 @changesets = @repository.changesets.find(:all,
77 @changesets = @repository.changesets.find(:all,
78 :limit => @changeset_pages.items_per_page,
78 :limit => @changeset_pages.items_per_page,
79 :offset => @changeset_pages.current.offset)
79 :offset => @changeset_pages.current.offset)
80
80
81 respond_to do |format|
81 respond_to do |format|
82 format.html { render :layout => false if request.xhr? }
82 format.html { render :layout => false if request.xhr? }
83 format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
83 format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
84 end
84 end
85 end
85 end
86
86
87 def entry
87 def entry
88 @content = @repository.scm.cat(@path, @rev)
88 @content = @repository.scm.cat(@path, @rev)
89 show_error and return unless @content
89 show_error and return unless @content
90 if 'raw' == params[:format]
90 if 'raw' == params[:format]
91 send_data @content, :filename => @path.split('/').last
91 send_data @content, :filename => @path.split('/').last
92 end
92 end
93 end
93 end
94
94
95 def revision
95 def revision
96 @changeset = @repository.changesets.find_by_revision(@rev)
96 @changeset = @repository.changesets.find_by_revision(@rev)
97 show_error and return unless @changeset
97 show_error and return unless @changeset
98 @changes_count = @changeset.changes.size
98 @changes_count = @changeset.changes.size
99 @changes_pages = Paginator.new self, @changes_count, 150, params['page']
99 @changes_pages = Paginator.new self, @changes_count, 150, params['page']
100 @changes = @changeset.changes.find(:all,
100 @changes = @changeset.changes.find(:all,
101 :limit => @changes_pages.items_per_page,
101 :limit => @changes_pages.items_per_page,
102 :offset => @changes_pages.current.offset)
102 :offset => @changes_pages.current.offset)
103
103
104 render :action => "revision", :layout => false if request.xhr?
104 render :action => "revision", :layout => false if request.xhr?
105 end
105 end
106
106
107 def diff
107 def diff
108 @rev_to = params[:rev_to] ? params[:rev_to].to_i : (@rev - 1)
108 @rev_to = params[:rev_to] ? params[:rev_to].to_i : (@rev - 1)
109 @diff_type = ('sbs' == params[:type]) ? 'sbs' : 'inline'
109 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
110 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
111
112 # Save diff type as user preference
113 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
114 User.current.pref[:diff_type] = @diff_type
115 User.current.preference.save
116 end
110
117
111 @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")
118 @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")
112 unless read_fragment(@cache_key)
119 unless read_fragment(@cache_key)
113 @diff = @repository.diff(@path, @rev, @rev_to, @diff_type)
120 @diff = @repository.diff(@path, @rev, @rev_to, @diff_type)
114 show_error and return unless @diff
121 show_error and return unless @diff
115 end
122 end
116 end
123 end
117
124
118 def stats
125 def stats
119 end
126 end
120
127
121 def graph
128 def graph
122 data = nil
129 data = nil
123 case params[:graph]
130 case params[:graph]
124 when "commits_per_month"
131 when "commits_per_month"
125 data = graph_commits_per_month(@repository)
132 data = graph_commits_per_month(@repository)
126 when "commits_per_author"
133 when "commits_per_author"
127 data = graph_commits_per_author(@repository)
134 data = graph_commits_per_author(@repository)
128 end
135 end
129 if data
136 if data
130 headers["Content-Type"] = "image/svg+xml"
137 headers["Content-Type"] = "image/svg+xml"
131 send_data(data, :type => "image/svg+xml", :disposition => "inline")
138 send_data(data, :type => "image/svg+xml", :disposition => "inline")
132 else
139 else
133 render_404
140 render_404
134 end
141 end
135 end
142 end
136
143
137 private
144 private
138 def find_project
145 def find_project
139 @project = Project.find(params[:id])
146 @project = Project.find(params[:id])
140 rescue ActiveRecord::RecordNotFound
147 rescue ActiveRecord::RecordNotFound
141 render_404
148 render_404
142 end
149 end
143
150
144 def find_repository
151 def find_repository
145 @project = Project.find(params[:id])
152 @project = Project.find(params[:id])
146 @repository = @project.repository
153 @repository = @project.repository
147 render_404 and return false unless @repository
154 render_404 and return false unless @repository
148 @path = params[:path].join('/') unless params[:path].nil?
155 @path = params[:path].join('/') unless params[:path].nil?
149 @path ||= ''
156 @path ||= ''
150 @rev = params[:rev].to_i if params[:rev]
157 @rev = params[:rev].to_i if params[:rev]
151 rescue ActiveRecord::RecordNotFound
158 rescue ActiveRecord::RecordNotFound
152 render_404
159 render_404
153 end
160 end
154
161
155 def show_error
162 def show_error
156 flash.now[:error] = l(:notice_scm_error)
163 flash.now[:error] = l(:notice_scm_error)
157 render :nothing => true, :layout => true
164 render :nothing => true, :layout => true
158 end
165 end
159
166
160 def graph_commits_per_month(repository)
167 def graph_commits_per_month(repository)
161 @date_to = Date.today
168 @date_to = Date.today
162 @date_from = @date_to << 11
169 @date_from = @date_to << 11
163 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
170 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
164 commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
171 commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
165 commits_by_month = [0] * 12
172 commits_by_month = [0] * 12
166 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
173 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
167
174
168 changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
175 changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
169 changes_by_month = [0] * 12
176 changes_by_month = [0] * 12
170 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
177 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
171
178
172 fields = []
179 fields = []
173 month_names = l(:actionview_datehelper_select_month_names_abbr).split(',')
180 month_names = l(:actionview_datehelper_select_month_names_abbr).split(',')
174 12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]}
181 12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]}
175
182
176 graph = SVG::Graph::Bar.new(
183 graph = SVG::Graph::Bar.new(
177 :height => 300,
184 :height => 300,
178 :width => 500,
185 :width => 500,
179 :fields => fields.reverse,
186 :fields => fields.reverse,
180 :stack => :side,
187 :stack => :side,
181 :scale_integers => true,
188 :scale_integers => true,
182 :step_x_labels => 2,
189 :step_x_labels => 2,
183 :show_data_values => false,
190 :show_data_values => false,
184 :graph_title => l(:label_commits_per_month),
191 :graph_title => l(:label_commits_per_month),
185 :show_graph_title => true
192 :show_graph_title => true
186 )
193 )
187
194
188 graph.add_data(
195 graph.add_data(
189 :data => commits_by_month[0..11].reverse,
196 :data => commits_by_month[0..11].reverse,
190 :title => l(:label_revision_plural)
197 :title => l(:label_revision_plural)
191 )
198 )
192
199
193 graph.add_data(
200 graph.add_data(
194 :data => changes_by_month[0..11].reverse,
201 :data => changes_by_month[0..11].reverse,
195 :title => l(:label_change_plural)
202 :title => l(:label_change_plural)
196 )
203 )
197
204
198 graph.burn
205 graph.burn
199 end
206 end
200
207
201 def graph_commits_per_author(repository)
208 def graph_commits_per_author(repository)
202 commits_by_author = repository.changesets.count(:all, :group => :committer)
209 commits_by_author = repository.changesets.count(:all, :group => :committer)
203 commits_by_author.sort! {|x, y| x.last <=> y.last}
210 commits_by_author.sort! {|x, y| x.last <=> y.last}
204
211
205 changes_by_author = repository.changes.count(:all, :group => :committer)
212 changes_by_author = repository.changes.count(:all, :group => :committer)
206 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
213 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
207
214
208 fields = commits_by_author.collect {|r| r.first}
215 fields = commits_by_author.collect {|r| r.first}
209 commits_data = commits_by_author.collect {|r| r.last}
216 commits_data = commits_by_author.collect {|r| r.last}
210 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
217 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
211
218
212 fields = fields + [""]*(10 - fields.length) if fields.length<10
219 fields = fields + [""]*(10 - fields.length) if fields.length<10
213 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
220 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
214 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
221 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
215
222
216 graph = SVG::Graph::BarHorizontal.new(
223 graph = SVG::Graph::BarHorizontal.new(
217 :height => 300,
224 :height => 300,
218 :width => 500,
225 :width => 500,
219 :fields => fields,
226 :fields => fields,
220 :stack => :side,
227 :stack => :side,
221 :scale_integers => true,
228 :scale_integers => true,
222 :show_data_values => false,
229 :show_data_values => false,
223 :rotate_y_labels => false,
230 :rotate_y_labels => false,
224 :graph_title => l(:label_commits_per_author),
231 :graph_title => l(:label_commits_per_author),
225 :show_graph_title => true
232 :show_graph_title => true
226 )
233 )
227
234
228 graph.add_data(
235 graph.add_data(
229 :data => commits_data,
236 :data => commits_data,
230 :title => l(:label_revision_plural)
237 :title => l(:label_revision_plural)
231 )
238 )
232
239
233 graph.add_data(
240 graph.add_data(
234 :data => changes_data,
241 :data => changes_data,
235 :title => l(:label_change_plural)
242 :title => l(:label_change_plural)
236 )
243 )
237
244
238 graph.burn
245 graph.burn
239 end
246 end
240
247
241 end
248 end
242
249
243 class Date
250 class Date
244 def months_ago(date = Date.today)
251 def months_ago(date = Date.today)
245 (date.year - self.year)*12 + (date.month - self.month)
252 (date.year - self.year)*12 + (date.month - self.month)
246 end
253 end
247
254
248 def weeks_ago(date = Date.today)
255 def weeks_ago(date = Date.today)
249 (date.year - self.year)*52 + (date.cweek - self.cweek)
256 (date.year - self.year)*52 + (date.cweek - self.cweek)
250 end
257 end
251 end
258 end
252
259
253 class String
260 class String
254 def with_leading_slash
261 def with_leading_slash
255 starts_with?('/') ? self : "/#{self}"
262 starts_with?('/') ? self : "/#{self}"
256 end
263 end
257 end
264 end
@@ -1,93 +1,93
1 <h2><%= l(:label_revision) %> <%= @rev %>: <%= @path.gsub(/^.*\//, '') %></h2>
1 <h2><%= l(:label_revision) %> <%= @rev %>: <%= @path.gsub(/^.*\//, '') %></h2>
2
2
3 <!-- Choose view type -->
3 <!-- Choose view type -->
4 <% form_tag({ :controller => 'repositories', :action => 'diff'}, :method => 'get') do %>
4 <% form_tag({ :controller => 'repositories', :action => 'diff'}, :method => 'get') do %>
5 <% params.each do |k, p| %>
5 <% params.each do |k, p| %>
6 <% if k != "type" %>
6 <% if k != "type" %>
7 <%= hidden_field_tag(k,p) %>
7 <%= hidden_field_tag(k,p) %>
8 <% end %>
8 <% end %>
9 <% end %>
9 <% end %>
10 <p><label><%= l(:label_view_diff) %></label>
10 <p><label><%= l(:label_view_diff) %></label>
11 <%= select_tag 'type', options_for_select([[l(:label_diff_inline), "inline"], [l(:label_diff_side_by_side), "sbs"]], params[:type]), :onchange => "if (this.value != '') {this.form.submit()}" %></p>
11 <%= select_tag 'type', options_for_select([[l(:label_diff_inline), "inline"], [l(:label_diff_side_by_side), "sbs"]], @diff_type), :onchange => "if (this.value != '') {this.form.submit()}" %></p>
12 <% end %>
12 <% end %>
13
13
14 <div class="autoscroll">
14 <div class="autoscroll">
15 <% cache(@cache_key) do %>
15 <% cache(@cache_key) do %>
16 <% @diff.each do |table_file| %>
16 <% @diff.each do |table_file| %>
17 <% if @diff_type == 'sbs' %>
17 <% if @diff_type == 'sbs' %>
18 <table class="filecontent CodeRay">
18 <table class="filecontent CodeRay">
19 <thead>
19 <thead>
20 <tr>
20 <tr>
21 <th colspan="4" class="filename">
21 <th colspan="4" class="filename">
22 <%= table_file.file_name %>
22 <%= table_file.file_name %>
23 </th>
23 </th>
24 </tr>
24 </tr>
25 <tr>
25 <tr>
26 <th colspan="2">@<%= @rev %></th>
26 <th colspan="2">@<%= @rev %></th>
27 <th colspan="2">@<%= @rev_to %></th>
27 <th colspan="2">@<%= @rev_to %></th>
28 </tr>
28 </tr>
29 </thead>
29 </thead>
30 <tbody>
30 <tbody>
31 <% table_file.keys.sort.each do |key| %>
31 <% table_file.keys.sort.each do |key| %>
32 <tr>
32 <tr>
33 <th class="line-num">
33 <th class="line-num">
34 <%= table_file[key].nb_line_left %>
34 <%= table_file[key].nb_line_left %>
35 </th>
35 </th>
36 <td class="line-code <%= table_file[key].type_diff_left %>">
36 <td class="line-code <%= table_file[key].type_diff_left %>">
37 <pre><%=to_utf8 table_file[key].line_left %></pre>
37 <pre><%=to_utf8 table_file[key].line_left %></pre>
38 </td>
38 </td>
39 <th class="line-num">
39 <th class="line-num">
40 <%= table_file[key].nb_line_right %>
40 <%= table_file[key].nb_line_right %>
41 </th>
41 </th>
42 <td class="line-code <%= table_file[key].type_diff_right %>">
42 <td class="line-code <%= table_file[key].type_diff_right %>">
43 <pre><%=to_utf8 table_file[key].line_right %></pre>
43 <pre><%=to_utf8 table_file[key].line_right %></pre>
44 </td>
44 </td>
45 </tr>
45 </tr>
46 <% end %>
46 <% end %>
47 </tbody>
47 </tbody>
48 </table>
48 </table>
49
49
50 <% else %>
50 <% else %>
51 <table class="filecontent CodeRay">
51 <table class="filecontent CodeRay">
52 <thead>
52 <thead>
53 <tr>
53 <tr>
54 <th colspan="3" class="filename">
54 <th colspan="3" class="filename">
55 <%= table_file.file_name %>
55 <%= table_file.file_name %>
56 </th>
56 </th>
57 </tr>
57 </tr>
58 <tr>
58 <tr>
59 <th>@<%= @rev %></th>
59 <th>@<%= @rev %></th>
60 <th>@<%= @rev_to %></th>
60 <th>@<%= @rev_to %></th>
61 <th></th>
61 <th></th>
62 </tr>
62 </tr>
63 </thead>
63 </thead>
64 <tbody>
64 <tbody>
65 <% table_file.keys.sort.each do |key, line| %>
65 <% table_file.keys.sort.each do |key, line| %>
66 <tr>
66 <tr>
67 <th class="line-num">
67 <th class="line-num">
68 <%= table_file[key].nb_line_left %>
68 <%= table_file[key].nb_line_left %>
69 </th>
69 </th>
70 <th class="line-num">
70 <th class="line-num">
71 <%= table_file[key].nb_line_right %>
71 <%= table_file[key].nb_line_right %>
72 </th>
72 </th>
73 <% if table_file[key].line_left.empty? %>
73 <% if table_file[key].line_left.empty? %>
74 <td class="line-code <%= table_file[key].type_diff_right %>">
74 <td class="line-code <%= table_file[key].type_diff_right %>">
75 <pre><%=to_utf8 table_file[key].line_right %></pre>
75 <pre><%=to_utf8 table_file[key].line_right %></pre>
76 </td>
76 </td>
77 <% else %>
77 <% else %>
78 <td class="line-code <%= table_file[key].type_diff_left %>">
78 <td class="line-code <%= table_file[key].type_diff_left %>">
79 <pre><%=to_utf8 table_file[key].line_left %></pre>
79 <pre><%=to_utf8 table_file[key].line_left %></pre>
80 </td>
80 </td>
81 <% end %>
81 <% end %>
82 </tr>
82 </tr>
83 <% end %>
83 <% end %>
84 </tbody>
84 </tbody>
85 </table>
85 </table>
86 <% end %>
86 <% end %>
87 <% end %>
87 <% end %>
88 <% end %>
88 <% end %>
89 </div>
89 </div>
90
90
91 <% content_for :header_tags do %>
91 <% content_for :header_tags do %>
92 <%= stylesheet_link_tag "scm" %>
92 <%= stylesheet_link_tag "scm" %>
93 <% end %>
93 <% end %>
@@ -1,132 +1,132
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19
19
20 class UserTest < Test::Unit::TestCase
20 class UserTest < Test::Unit::TestCase
21 fixtures :users, :members, :projects
21 fixtures :users, :members, :projects
22
22
23 def setup
23 def setup
24 @admin = User.find(1)
24 @admin = User.find(1)
25 @jsmith = User.find(2)
25 @jsmith = User.find(2)
26 @dlopper = User.find(3)
26 @dlopper = User.find(3)
27 end
27 end
28
28
29 def test_truth
29 def test_truth
30 assert_kind_of User, @jsmith
30 assert_kind_of User, @jsmith
31 end
31 end
32
32
33 def test_create
33 def test_create
34 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
34 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
35
35
36 user.login = "jsmith"
36 user.login = "jsmith"
37 user.password, user.password_confirmation = "password", "password"
37 user.password, user.password_confirmation = "password", "password"
38 # login uniqueness
38 # login uniqueness
39 assert !user.save
39 assert !user.save
40 assert_equal 1, user.errors.count
40 assert_equal 1, user.errors.count
41
41
42 user.login = "newuser"
42 user.login = "newuser"
43 user.password, user.password_confirmation = "passwd", "password"
43 user.password, user.password_confirmation = "passwd", "password"
44 # password confirmation
44 # password confirmation
45 assert !user.save
45 assert !user.save
46 assert_equal 1, user.errors.count
46 assert_equal 1, user.errors.count
47
47
48 user.password, user.password_confirmation = "password", "password"
48 user.password, user.password_confirmation = "password", "password"
49 assert user.save
49 assert user.save
50 end
50 end
51
51
52 def test_update
52 def test_update
53 assert_equal "admin", @admin.login
53 assert_equal "admin", @admin.login
54 @admin.login = "john"
54 @admin.login = "john"
55 assert @admin.save, @admin.errors.full_messages.join("; ")
55 assert @admin.save, @admin.errors.full_messages.join("; ")
56 @admin.reload
56 @admin.reload
57 assert_equal "john", @admin.login
57 assert_equal "john", @admin.login
58 end
58 end
59
59
60 def test_validate
60 def test_validate
61 @admin.login = ""
61 @admin.login = ""
62 assert !@admin.save
62 assert !@admin.save
63 assert_equal 2, @admin.errors.count
63 assert_equal 2, @admin.errors.count
64 end
64 end
65
65
66 def test_password
66 def test_password
67 user = User.try_to_login("admin", "admin")
67 user = User.try_to_login("admin", "admin")
68 assert_kind_of User, user
68 assert_kind_of User, user
69 assert_equal "admin", user.login
69 assert_equal "admin", user.login
70 user.password = "hello"
70 user.password = "hello"
71 assert user.save
71 assert user.save
72
72
73 user = User.try_to_login("admin", "hello")
73 user = User.try_to_login("admin", "hello")
74 assert_kind_of User, user
74 assert_kind_of User, user
75 assert_equal "admin", user.login
75 assert_equal "admin", user.login
76 assert_equal User.hash_password("hello"), user.hashed_password
76 assert_equal User.hash_password("hello"), user.hashed_password
77 end
77 end
78
78
79 def test_lock
79 def test_lock
80 user = User.try_to_login("jsmith", "jsmith")
80 user = User.try_to_login("jsmith", "jsmith")
81 assert_equal @jsmith, user
81 assert_equal @jsmith, user
82
82
83 @jsmith.status = User::STATUS_LOCKED
83 @jsmith.status = User::STATUS_LOCKED
84 assert @jsmith.save
84 assert @jsmith.save
85
85
86 user = User.try_to_login("jsmith", "jsmith")
86 user = User.try_to_login("jsmith", "jsmith")
87 assert_equal nil, user
87 assert_equal nil, user
88 end
88 end
89
89
90 def test_rss_key
90 def test_rss_key
91 assert_nil @jsmith.rss_token
91 assert_nil @jsmith.rss_token
92 key = @jsmith.rss_key
92 key = @jsmith.rss_key
93 assert_equal 40, key.length
93 assert_equal 40, key.length
94
94
95 @jsmith.reload
95 @jsmith.reload
96 assert_equal key, @jsmith.rss_key
96 assert_equal key, @jsmith.rss_key
97 end
97 end
98
98
99 def test_role_for_project
99 def test_role_for_project
100 # user with a role
100 # user with a role
101 role = @jsmith.role_for_project(Project.find(1))
101 role = @jsmith.role_for_project(Project.find(1))
102 assert_kind_of Role, role
102 assert_kind_of Role, role
103 assert_equal "Manager", role.name
103 assert_equal "Manager", role.name
104
104
105 # user with no role
105 # user with no role
106 assert !@dlopper.role_for_project(Project.find(2)).member?
106 assert !@dlopper.role_for_project(Project.find(2)).member?
107 end
107 end
108
108
109 def test_mail_notification_all
109 def test_mail_notification_all
110 @jsmith.mail_notification = true
110 @jsmith.mail_notification = true
111 @jsmith.notified_project_ids = []
111 @jsmith.notified_project_ids = []
112 @jsmith.save
112 @jsmith.save
113 @jsmith.reload
113 @jsmith.reload
114 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
114 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
115 end
115 end
116
116
117 def test_mail_notification_selected
117 def test_mail_notification_selected
118 @jsmith.mail_notification = false
118 @jsmith.mail_notification = false
119 @jsmith.notified_project_ids = [@jsmith.projects.first.id]
119 @jsmith.notified_project_ids = [1]
120 @jsmith.save
120 @jsmith.save
121 @jsmith.reload
121 @jsmith.reload
122 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
122 assert Project.find(1).recipients.include?(@jsmith.mail)
123 end
123 end
124
124
125 def test_mail_notification_none
125 def test_mail_notification_none
126 @jsmith.mail_notification = false
126 @jsmith.mail_notification = false
127 @jsmith.notified_project_ids = []
127 @jsmith.notified_project_ids = []
128 @jsmith.save
128 @jsmith.save
129 @jsmith.reload
129 @jsmith.reload
130 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
130 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
131 end
131 end
132 end
132 end
General Comments 0
You need to be logged in to leave comments. Login now