From 04376c286daee0165f6544ecdafaa645aa7695c0 Mon Sep 17 00:00:00 2001 From: Azul Date: Sat, 22 Dec 2012 15:51:24 +0100 Subject: moving the js for view definitions into separate files This way we get syntax highlighting and so on. --- core/lib/extensions/couchrest.rb | 25 +++++-- help/app/designs/ticket/by_includes_post_by.js | 13 ++++ .../ticket/by_includes_post_by_and_created_at.js | 12 ++++ ..._includes_post_by_and_is_open_and_created_at.js | 12 ++++ ..._includes_post_by_and_is_open_and_updated_at.js | 12 ++++ .../ticket/by_includes_post_by_and_updated_at.js | 12 ++++ help/app/models/ticket.rb | 79 +--------------------- users/app/designs/user/by_email_alias.js | 8 +++ users/app/designs/user/by_email_or_alias.js | 11 +++ users/app/models/user.rb | 29 +------- 10 files changed, 101 insertions(+), 112 deletions(-) create mode 100644 help/app/designs/ticket/by_includes_post_by.js create mode 100644 help/app/designs/ticket/by_includes_post_by_and_created_at.js create mode 100644 help/app/designs/ticket/by_includes_post_by_and_is_open_and_created_at.js create mode 100644 help/app/designs/ticket/by_includes_post_by_and_is_open_and_updated_at.js create mode 100644 help/app/designs/ticket/by_includes_post_by_and_updated_at.js create mode 100644 users/app/designs/user/by_email_alias.js create mode 100644 users/app/designs/user/by_email_or_alias.js diff --git a/core/lib/extensions/couchrest.rb b/core/lib/extensions/couchrest.rb index a8da23b..0dca632 100644 --- a/core/lib/extensions/couchrest.rb +++ b/core/lib/extensions/couchrest.rb @@ -1,8 +1,21 @@ -class CouchRest::Model::Designs::View - - # so we can called Ticket.method.descending or Ticket.method.ascending - def ascending - self - end +module CouchRest::Model::Designs + + class View + + # so we can called Ticket.method.descending or Ticket.method.ascending + def ascending + self + end + end + + class DesignMapper + def load_views(dir) + Dir.glob("#{dir}/*.js") do |js| + name = File.basename(js, '.js') + file = File.open(js, 'r') + view name.to_sym, :map => file.read + end + end + end end diff --git a/help/app/designs/ticket/by_includes_post_by.js b/help/app/designs/ticket/by_includes_post_by.js new file mode 100644 index 0000000..2eeac89 --- /dev/null +++ b/help/app/designs/ticket/by_includes_post_by.js @@ -0,0 +1,13 @@ +// TODO: This view is only used in tests--should we keep it? +function(doc) { + var arr = {} + if (doc['type'] == 'Ticket' && doc.comments) { + doc.comments.forEach(function(comment){ + if (comment.posted_by && !arr[comment.posted_by]) { + //don't add duplicates + arr[comment.posted_by] = true; + emit(comment.posted_by, 1); + } + }); + } +} diff --git a/help/app/designs/ticket/by_includes_post_by_and_created_at.js b/help/app/designs/ticket/by_includes_post_by_and_created_at.js new file mode 100644 index 0000000..72169b0 --- /dev/null +++ b/help/app/designs/ticket/by_includes_post_by_and_created_at.js @@ -0,0 +1,12 @@ +function(doc) { + var arr = {} + if (doc['type'] == 'Ticket' && doc.comments) { + doc.comments.forEach(function(comment){ + if (comment.posted_by && !arr[comment.posted_by]) { + //don't add duplicates + arr[comment.posted_by] = true; + emit([comment.posted_by, doc.created_at], 1); + } + }); + } +} diff --git a/help/app/designs/ticket/by_includes_post_by_and_is_open_and_created_at.js b/help/app/designs/ticket/by_includes_post_by_and_is_open_and_created_at.js new file mode 100644 index 0000000..33dfe0b --- /dev/null +++ b/help/app/designs/ticket/by_includes_post_by_and_is_open_and_created_at.js @@ -0,0 +1,12 @@ +function(doc) { + var arr = {} + if (doc['type'] == 'Ticket' && doc.comments) { + doc.comments.forEach(function(comment){ + if (comment.posted_by && !arr[comment.posted_by]) { + //don't add duplicates + arr[comment.posted_by] = true; + emit([comment.posted_by, doc.is_open, doc.created_at], 1); + } + }); + } +} diff --git a/help/app/designs/ticket/by_includes_post_by_and_is_open_and_updated_at.js b/help/app/designs/ticket/by_includes_post_by_and_is_open_and_updated_at.js new file mode 100644 index 0000000..3bd2a74 --- /dev/null +++ b/help/app/designs/ticket/by_includes_post_by_and_is_open_and_updated_at.js @@ -0,0 +1,12 @@ +function(doc) { + var arr = {} + if (doc['type'] == 'Ticket' && doc.comments) { + doc.comments.forEach(function(comment){ + if (comment.posted_by && !arr[comment.posted_by]) { + //don't add duplicates + arr[comment.posted_by] = true; + emit([comment.posted_by, doc.is_open, doc.updated_at], 1); + } + }); + } +} diff --git a/help/app/designs/ticket/by_includes_post_by_and_updated_at.js b/help/app/designs/ticket/by_includes_post_by_and_updated_at.js new file mode 100644 index 0000000..2b4304f --- /dev/null +++ b/help/app/designs/ticket/by_includes_post_by_and_updated_at.js @@ -0,0 +1,12 @@ +function(doc) { + var arr = {} + if (doc['type'] == 'Ticket' && doc.comments) { + doc.comments.forEach(function(comment){ + if (comment.posted_by && !arr[comment.posted_by]) { + //don't add duplicates + arr[comment.posted_by] = true; + emit([comment.posted_by, doc.updated_at], 1); + } + }); + } +} diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index fa056b4..a5c9ee5 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -47,84 +47,7 @@ class Ticket < CouchRest::Model::Base view :by_is_open_and_created_at view :by_is_open_and_updated_at - - #TODO: This view is only used in tests--should we keep it? - view :by_includes_post_by, - :map => - "function(doc) { - var arr = {} - if (doc['type'] == 'Ticket' && doc.comments) { - doc.comments.forEach(function(comment){ - if (comment.posted_by && !arr[comment.posted_by]) { - //don't add duplicates - arr[comment.posted_by] = true; - emit(comment.posted_by, 1); - } - }); - } - }", :reduce => "function(k,v,r) { return sum(v); }" - - view :by_includes_post_by_and_is_open_and_updated_at, - :map => - "function(doc) { - var arr = {} - if (doc['type'] == 'Ticket' && doc.comments) { - doc.comments.forEach(function(comment){ - if (comment.posted_by && !arr[comment.posted_by]) { - //don't add duplicates - arr[comment.posted_by] = true; - emit([comment.posted_by, doc.is_open, doc.updated_at], 1); - } - }); - } - }", :reduce => "function(k,v,r) { return sum(v); }" - - view :by_includes_post_by_and_is_open_and_created_at, - :map => - "function(doc) { - var arr = {} - if (doc['type'] == 'Ticket' && doc.comments) { - doc.comments.forEach(function(comment){ - if (comment.posted_by && !arr[comment.posted_by]) { - //don't add duplicates - arr[comment.posted_by] = true; - emit([comment.posted_by, doc.is_open, doc.created_at], 1); - } - }); - } - }", :reduce => "function(k,v,r) { return sum(v); }" - - view :by_includes_post_by_and_updated_at, - :map => - "function(doc) { - var arr = {} - if (doc['type'] == 'Ticket' && doc.comments) { - doc.comments.forEach(function(comment){ - if (comment.posted_by && !arr[comment.posted_by]) { - //don't add duplicates - arr[comment.posted_by] = true; - emit([comment.posted_by, doc.updated_at], 1); - } - }); - } - }", :reduce => "function(k,v,r) { return sum(v); }" - - - view :by_includes_post_by_and_created_at, - :map => - "function(doc) { - var arr = {} - if (doc['type'] == 'Ticket' && doc.comments) { - doc.comments.forEach(function(comment){ - if (comment.posted_by && !arr[comment.posted_by]) { - //don't add duplicates - arr[comment.posted_by] = true; - emit([comment.posted_by, doc.created_at], 1); - } - }); - } - }", :reduce => "function(k,v,r) { return sum(v); }" - + load_views(Rails.root.join('help', 'app', 'designs', 'ticket')) end validates :title, :presence => true diff --git a/users/app/designs/user/by_email_alias.js b/users/app/designs/user/by_email_alias.js new file mode 100644 index 0000000..51eb976 --- /dev/null +++ b/users/app/designs/user/by_email_alias.js @@ -0,0 +1,8 @@ +function(doc) { + if (doc.type != 'User') { + return; + } + doc.email_aliases.forEach(function(alias){ + emit(alias.email, doc); + }); +} diff --git a/users/app/designs/user/by_email_or_alias.js b/users/app/designs/user/by_email_or_alias.js new file mode 100644 index 0000000..2f1e569 --- /dev/null +++ b/users/app/designs/user/by_email_or_alias.js @@ -0,0 +1,11 @@ +function(doc) { + if (doc.type != 'User') { + return; + } + if (doc.email) { + emit(doc.email, doc); + } + doc.email_aliases.forEach(function(alias){ + emit(alias.email, doc); + }); +} diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 2a8a57b..1798ea4 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -46,37 +46,10 @@ class User < CouchRest::Model::Base timestamps! design do + load_views(Rails.root.join('users', 'app', 'designs', 'user')) view :by_login view :by_created_at view :by_email - - view :by_email_alias, - :map => <<-EOJS - function(doc) { - if (doc.type != 'User') { - return; - } - doc.email_aliases.forEach(function(alias){ - emit(alias.email, doc); - }); - } - EOJS - - view :by_email_or_alias, - :map => <<-EOJS - function(doc) { - if (doc.type != 'User') { - return; - } - if (doc.email) { - emit(doc.email, doc); - } - doc.email_aliases.forEach(function(alias){ - emit(alias.email, doc); - }); - } - EOJS - end class << self -- cgit v1.2.3 From bf80482c34034a59307193ced0dcfac7db05f055 Mon Sep 17 00:00:00 2001 From: jessib Date: Mon, 31 Dec 2012 13:05:39 -0800 Subject: Client-side validations, including only validating email address format if something is input for email address. --- help/app/models/ticket.rb | 2 +- help/app/views/tickets/_new_comment.html.haml | 2 +- help/app/views/tickets/new.html.haml | 2 +- help/app/views/tickets/show.html.haml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index fa056b4..0399b4e 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -132,7 +132,7 @@ class Ticket < CouchRest::Model::Base # html5 has built-in validation which isn't ideal, as it says 'please enter an email address' for invalid email addresses, which implies an email address is required, and it is not. - validates :email, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, :if => :email #email address is optional + validates :email, :allow_blank => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/ #email address is optional #TODO: #def set_created_by diff --git a/help/app/views/tickets/_new_comment.html.haml b/help/app/views/tickets/_new_comment.html.haml index 7307dad..31d134f 100644 --- a/help/app/views/tickets/_new_comment.html.haml +++ b/help/app/views/tickets/_new_comment.html.haml @@ -1,2 +1,2 @@ = f.simple_fields_for :comments, @comment do |c| - = c.input :body, :label => 'Comment', :as => :text, :input_html => {:class => "span12", :rows=>4} + = c.input :body, :label => 'Comment', :as => :text, :input_html => {:class => "span9", :rows=>4} diff --git a/help/app/views/tickets/new.html.haml b/help/app/views/tickets/new.html.haml index 750b990..ee7adb2 100644 --- a/help/app/views/tickets/new.html.haml +++ b/help/app/views/tickets/new.html.haml @@ -1,6 +1,6 @@ .span12 %h2=t :new_ticket - = simple_form_for(@ticket, :html => {:novalidate => true}) do |f| #turn off html5 validations to test + = simple_form_for @ticket, :validate => true, :html => {:class => 'form-horizontal'} do |f| = f.input :title = f.input :email if !current_user #hmm--might authenticated users want to submit an alternate email? = render :partial => 'new_comment', :locals => {:f => f} diff --git a/help/app/views/tickets/show.html.haml b/help/app/views/tickets/show.html.haml index 3f00b35..a69048b 100644 --- a/help/app/views/tickets/show.html.haml +++ b/help/app/views/tickets/show.html.haml @@ -8,7 +8,7 @@ = render(:partial => "comment", :collection => @ticket.comments) = #render @ticket.comments should work if view is in /app/views/comments/_comment - = simple_form_for(@ticket, :html => {:novalidate => true}) do |f| #turn off html5 validations to test + = simple_form_for @ticket, :html => {:class => 'form-horizontal'} do |f| # don't need validations so long as this is so simple = render :partial => 'new_comment', :locals => {:f => f} .span10.offset3 = f.button :submit, @post_reply_str, :class => 'btn-primary' -- cgit v1.2.3 From fd62997599f327c0153e23c9f8d2b074a83b43a7 Mon Sep 17 00:00:00 2001 From: jessib Date: Mon, 31 Dec 2012 13:16:01 -0800 Subject: Small CSS tweak --- help/app/views/tickets/_comment.html.haml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/help/app/views/tickets/_comment.html.haml b/help/app/views/tickets/_comment.html.haml index 26794dc..1d8ee41 100644 --- a/help/app/views/tickets/_comment.html.haml +++ b/help/app/views/tickets/_comment.html.haml @@ -6,7 +6,8 @@ = 'Posted by' + (commenter.is_admin? ? ' admin' : '') + ':' = commenter.login - else - Unauthenticated post + %b + Unauthenticated post .pull-right %b Posted at: -- cgit v1.2.3 From 754d048d4b927fc569721d5fe238b23285841804 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 3 Jan 2013 10:34:56 +0100 Subject: using leap_ca for cert model, removing empty dirs --- certs/app/assets/images/leap_web_certs/.gitkeep | 0 .../app/assets/javascripts/leap_web_certs/.gitkeep | 0 .../app/assets/stylesheets/leap_web_certs/.gitkeep | 0 certs/app/helpers/.gitkeep | 0 certs/app/helpers/certs_helper.rb | 2 - certs/app/mailers/.gitkeep | 0 certs/app/models/.gitkeep | 0 certs/app/models/leap_ca/cert.rb | 56 ---------------------- certs/app/views/.gitkeep | 0 certs/leap_web_certs.gemspec | 1 + certs/lib/leap_web_certs/engine.rb | 1 + 11 files changed, 2 insertions(+), 58 deletions(-) delete mode 100644 certs/app/assets/images/leap_web_certs/.gitkeep delete mode 100644 certs/app/assets/javascripts/leap_web_certs/.gitkeep delete mode 100644 certs/app/assets/stylesheets/leap_web_certs/.gitkeep delete mode 100644 certs/app/helpers/.gitkeep delete mode 100644 certs/app/helpers/certs_helper.rb delete mode 100644 certs/app/mailers/.gitkeep delete mode 100644 certs/app/models/.gitkeep delete mode 100644 certs/app/models/leap_ca/cert.rb delete mode 100644 certs/app/views/.gitkeep diff --git a/certs/app/assets/images/leap_web_certs/.gitkeep b/certs/app/assets/images/leap_web_certs/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/certs/app/assets/javascripts/leap_web_certs/.gitkeep b/certs/app/assets/javascripts/leap_web_certs/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/certs/app/assets/stylesheets/leap_web_certs/.gitkeep b/certs/app/assets/stylesheets/leap_web_certs/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/certs/app/helpers/.gitkeep b/certs/app/helpers/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/certs/app/helpers/certs_helper.rb b/certs/app/helpers/certs_helper.rb deleted file mode 100644 index 94e76b8..0000000 --- a/certs/app/helpers/certs_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module CertsHelper -end diff --git a/certs/app/mailers/.gitkeep b/certs/app/mailers/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/certs/app/models/.gitkeep b/certs/app/models/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/certs/app/models/leap_ca/cert.rb b/certs/app/models/leap_ca/cert.rb deleted file mode 100644 index 9d4f15e..0000000 --- a/certs/app/models/leap_ca/cert.rb +++ /dev/null @@ -1,56 +0,0 @@ -# -# Model for certificates stored in CouchDB. -# -# This file must be loaded after Config has been loaded. -# - -module LeapCA - class Cert < CouchRest::Model::Base - -# No config yet. use_database LeapCA::Config.db_name - use_database 'client_certificates' - - timestamps! - - property :key, String # the client private RSA key - property :cert, String # the client x509 certificate, signed by the CA - property :valid_until, Time # expiration time of the client certificate - property :random, Float, :accessible => false # used to help pick a random cert by the webapp - - before_validation :set_random, :on => :create - - validates :key, :presence => true - validates :cert, :presence => true - validates :random, :presence => true - validates :random, :numericality => {:greater_than => 0, :less_than => 1} - - design do - view :by_random - end - - def set_random - self.random = rand - end - - class << self - def sample - self.by_random.startkey(rand).first || self.by_random.first - end - - def pick_from_pool - cert = self.sample - raise RECORD_NOT_FOUND unless cert - cert.destroy - return cert - rescue RESOURCE_NOT_FOUND - retry if self.by_random.count > 0 - raise RECORD_NOT_FOUND - end - - def valid_attributes_hash - {:key => "ABCD", :cert => "A123"} - end - end - - end -end diff --git a/certs/app/views/.gitkeep b/certs/app/views/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/certs/leap_web_certs.gemspec b/certs/leap_web_certs.gemspec index 15a45be..81bc042 100644 --- a/certs/leap_web_certs.gemspec +++ b/certs/leap_web_certs.gemspec @@ -16,5 +16,6 @@ Gem::Specification.new do |s| s.test_files = Dir["test/**/*"] s.add_dependency "leap_web_core", LeapWeb::VERSION + s.add_dependency "leap_ca", '~> 0.2.0' end diff --git a/certs/lib/leap_web_certs/engine.rb b/certs/lib/leap_web_certs/engine.rb index 3c8948a..118aee0 100644 --- a/certs/lib/leap_web_certs/engine.rb +++ b/certs/lib/leap_web_certs/engine.rb @@ -1,4 +1,5 @@ require "leap_web_core" +require "leap_ca" module LeapWebCerts class Engine < ::Rails::Engine -- cgit v1.2.3 From 5732c2fa48cf4af9af122ca5366f9ffffe5f22fb Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 3 Jan 2013 10:39:02 +0100 Subject: adding default reduce function to couch views read from files --- core/lib/extensions/couchrest.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/lib/extensions/couchrest.rb b/core/lib/extensions/couchrest.rb index 0dca632..5938df4 100644 --- a/core/lib/extensions/couchrest.rb +++ b/core/lib/extensions/couchrest.rb @@ -13,7 +13,9 @@ module CouchRest::Model::Designs Dir.glob("#{dir}/*.js") do |js| name = File.basename(js, '.js') file = File.open(js, 'r') - view name.to_sym, :map => file.read + view name.to_sym, + :map => file.read, + :reduce => "function(key, values, rereduce) { return sum(values); }" end end end -- cgit v1.2.3 From 1f249ecb60ef270278db4c39b78b1cc23425dce6 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 3 Jan 2013 12:03:16 +0100 Subject: removed cert unit tests as leap_ca now provides the model --- certs/test/unit/cert_pool_test.rb | 52 --------------------------------------- certs/test/unit/cert_test.rb | 39 ----------------------------- 2 files changed, 91 deletions(-) delete mode 100644 certs/test/unit/cert_pool_test.rb delete mode 100644 certs/test/unit/cert_test.rb diff --git a/certs/test/unit/cert_pool_test.rb b/certs/test/unit/cert_pool_test.rb deleted file mode 100644 index 06f7ce0..0000000 --- a/certs/test/unit/cert_pool_test.rb +++ /dev/null @@ -1,52 +0,0 @@ -require 'test_helper' - -class CertPoolTest < ActiveSupport::TestCase - - setup do - 2.times { LeapCA::Cert.create(LeapCA::Cert.valid_attributes_hash) } - end - - teardown do - LeapCA::Cert.all.each {|c| c.destroy} - end - - test "picks random sample" do - # with 3 certs chances are pretty low we pick the same one 40 times. - LeapCA::Cert.create! LeapCA::Cert.valid_attributes_hash - picked = [] - first = LeapCA::Cert.sample.id - current = LeapCA::Cert.sample.id - 40.times do - break if current != first - current = LeapCA::Cert.sample.id - end - assert_not_equal current, first - end - - test "picks cert from the pool" do - assert_difference "LeapCA::Cert.count", -1 do - cert = LeapCA::Cert.pick_from_pool - end - end - - test "err's out if all certs have been destroyed" do - sample = LeapCA::Cert.first.tap{|c| c.destroy} - LeapCA::Cert.all.each {|c| c.destroy} - assert_raises RECORD_NOT_FOUND do - LeapCA::Cert.expects(:sample).returns(sample) - cert = LeapCA::Cert.pick_from_pool - end - end - - test "picks other cert if first pick has been destroyed" do - first = LeapCA::Cert.first.tap{|c| c.destroy} - second = LeapCA::Cert.first - LeapCA::Cert.expects(:sample).at_least_once. - returns(first). - then.returns(second) - cert = LeapCA::Cert.pick_from_pool - assert_equal second, cert - assert_nil LeapCA::Cert.first - end - -end diff --git a/certs/test/unit/cert_test.rb b/certs/test/unit/cert_test.rb deleted file mode 100644 index 0b21d0b..0000000 --- a/certs/test/unit/cert_test.rb +++ /dev/null @@ -1,39 +0,0 @@ -require 'test_helper' - -class CertTest < ActiveSupport::TestCase - - setup do - @sample = LeapCA::Cert.new LeapCA::Cert.valid_attributes_hash - end - - test "stub cert for testing is valid" do - assert @sample.valid? - end - - test "setting random on create validation" do - @sample.random = "asdf" - assert @sample.valid? - assert @sample.random.is_a? Float - assert @sample.random >= 0 - assert @sample.random < 1 - end - - test "validates random" do - @sample.save # make sure we are past the on_create - assert @sample.valid? - ["asdf", 1, 2, -0.1, nil, "asdf"].each do |invalid| - @sample.random = invalid - assert !@sample.valid?, "#{invalid} should not be a valid value for random" - end - end - - test "validates key" do - @sample.key = nil - assert !@sample.valid?, "Cert should require key" - end - - test "validates cert" do - @sample.cert = nil - assert !@sample.valid?, "Cert should require cert" - end -end -- cgit v1.2.3 From e6496b0f45cc0b487da7cb35a34b8e79037034c0 Mon Sep 17 00:00:00 2001 From: jessib Date: Thu, 3 Jan 2013 11:22:56 -0800 Subject: Removed unnecessary comment. --- help/app/models/ticket.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index ef7b2d6..a27a9d4 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -55,7 +55,7 @@ class Ticket < CouchRest::Model::Base # html5 has built-in validation which isn't ideal, as it says 'please enter an email address' for invalid email addresses, which implies an email address is required, and it is not. - validates :email, :allow_blank => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/ #email address is optional + validates :email, :allow_blank => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/ #TODO: #def set_created_by -- cgit v1.2.3 From fbb8c7b654a05bc2983a476df9a5b9c376096f41 Mon Sep 17 00:00:00 2001 From: Azul Date: Sun, 6 Jan 2013 18:21:38 +0100 Subject: views only emit 1 as value now, doc can be included --- users/app/designs/user/by_email_alias.js | 2 +- users/app/designs/user/by_email_or_alias.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/users/app/designs/user/by_email_alias.js b/users/app/designs/user/by_email_alias.js index 51eb976..508a002 100644 --- a/users/app/designs/user/by_email_alias.js +++ b/users/app/designs/user/by_email_alias.js @@ -3,6 +3,6 @@ function(doc) { return; } doc.email_aliases.forEach(function(alias){ - emit(alias.email, doc); + emit(alias.email, 1); }); } diff --git a/users/app/designs/user/by_email_or_alias.js b/users/app/designs/user/by_email_or_alias.js index 2f1e569..71fd0ea 100644 --- a/users/app/designs/user/by_email_or_alias.js +++ b/users/app/designs/user/by_email_or_alias.js @@ -3,9 +3,9 @@ function(doc) { return; } if (doc.email) { - emit(doc.email, doc); + emit(doc.email, 1); } doc.email_aliases.forEach(function(alias){ - emit(alias.email, doc); + emit(alias.email, 1); }); } -- cgit v1.2.3 From 48258a0df7d4715a2b5533d94ef0f5906f08ea96 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 7 Jan 2013 20:06:57 +0100 Subject: prevent requiring leap_ca database settings --- certs/lib/leap_web_certs/engine.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/certs/lib/leap_web_certs/engine.rb b/certs/lib/leap_web_certs/engine.rb index 118aee0..471c648 100644 --- a/certs/lib/leap_web_certs/engine.rb +++ b/certs/lib/leap_web_certs/engine.rb @@ -1,5 +1,7 @@ require "leap_web_core" -require "leap_ca" +require "leap_ca/config" +LeapCA::Config.db_name = "client_certificates" +require "leap_ca/cert" module LeapWebCerts class Engine < ::Rails::Engine -- cgit v1.2.3 From da5718fe3bcc416dc12ec6892dd8a79ce37525d4 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 7 Jan 2013 21:03:51 +0100 Subject: attempting to work around server failure --- certs/lib/leap_web_certs/engine.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/certs/lib/leap_web_certs/engine.rb b/certs/lib/leap_web_certs/engine.rb index 471c648..bc0801d 100644 --- a/certs/lib/leap_web_certs/engine.rb +++ b/certs/lib/leap_web_certs/engine.rb @@ -1,6 +1,13 @@ require "leap_web_core" require "leap_ca/config" LeapCA::Config.db_name = "client_certificates" + +# couchrest model has an initializer for this - but apparently that does not work +CouchRest::Model::Base.configure do |conf| + conf.environment = Rails.env + conf.connection_config_file = File.join(Rails.root, 'config', 'couchdb.yml') +end + require "leap_ca/cert" module LeapWebCerts -- cgit v1.2.3 From cee6db281349789ba5ff6dc8d3dc6ca10400aebe Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 7 Jan 2013 21:21:16 +0100 Subject: Revert "Merge pull request #12 from leapcode/feature/use-leap_ca-lib" This reverts commit d2acd205b3c3f0931ce4c453e67b1ba572ec77e9, reversing changes made to e6496b0f45cc0b487da7cb35a34b8e79037034c0. Conflicts: certs/lib/leap_web_certs/engine.rb --- certs/app/assets/images/leap_web_certs/.gitkeep | 0 .../app/assets/javascripts/leap_web_certs/.gitkeep | 0 .../app/assets/stylesheets/leap_web_certs/.gitkeep | 0 certs/app/helpers/.gitkeep | 0 certs/app/helpers/certs_helper.rb | 2 + certs/app/mailers/.gitkeep | 0 certs/app/models/.gitkeep | 0 certs/app/models/leap_ca/cert.rb | 56 ++++++++++++++++++++++ certs/app/views/.gitkeep | 0 certs/leap_web_certs.gemspec | 1 - certs/lib/leap_web_certs/engine.rb | 10 ---- certs/test/unit/cert_pool_test.rb | 52 ++++++++++++++++++++ certs/test/unit/cert_test.rb | 39 +++++++++++++++ 13 files changed, 149 insertions(+), 11 deletions(-) create mode 100644 certs/app/assets/images/leap_web_certs/.gitkeep create mode 100644 certs/app/assets/javascripts/leap_web_certs/.gitkeep create mode 100644 certs/app/assets/stylesheets/leap_web_certs/.gitkeep create mode 100644 certs/app/helpers/.gitkeep create mode 100644 certs/app/helpers/certs_helper.rb create mode 100644 certs/app/mailers/.gitkeep create mode 100644 certs/app/models/.gitkeep create mode 100644 certs/app/models/leap_ca/cert.rb create mode 100644 certs/app/views/.gitkeep create mode 100644 certs/test/unit/cert_pool_test.rb create mode 100644 certs/test/unit/cert_test.rb diff --git a/certs/app/assets/images/leap_web_certs/.gitkeep b/certs/app/assets/images/leap_web_certs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/certs/app/assets/javascripts/leap_web_certs/.gitkeep b/certs/app/assets/javascripts/leap_web_certs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/certs/app/assets/stylesheets/leap_web_certs/.gitkeep b/certs/app/assets/stylesheets/leap_web_certs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/certs/app/helpers/.gitkeep b/certs/app/helpers/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/certs/app/helpers/certs_helper.rb b/certs/app/helpers/certs_helper.rb new file mode 100644 index 0000000..94e76b8 --- /dev/null +++ b/certs/app/helpers/certs_helper.rb @@ -0,0 +1,2 @@ +module CertsHelper +end diff --git a/certs/app/mailers/.gitkeep b/certs/app/mailers/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/certs/app/models/.gitkeep b/certs/app/models/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/certs/app/models/leap_ca/cert.rb b/certs/app/models/leap_ca/cert.rb new file mode 100644 index 0000000..9d4f15e --- /dev/null +++ b/certs/app/models/leap_ca/cert.rb @@ -0,0 +1,56 @@ +# +# Model for certificates stored in CouchDB. +# +# This file must be loaded after Config has been loaded. +# + +module LeapCA + class Cert < CouchRest::Model::Base + +# No config yet. use_database LeapCA::Config.db_name + use_database 'client_certificates' + + timestamps! + + property :key, String # the client private RSA key + property :cert, String # the client x509 certificate, signed by the CA + property :valid_until, Time # expiration time of the client certificate + property :random, Float, :accessible => false # used to help pick a random cert by the webapp + + before_validation :set_random, :on => :create + + validates :key, :presence => true + validates :cert, :presence => true + validates :random, :presence => true + validates :random, :numericality => {:greater_than => 0, :less_than => 1} + + design do + view :by_random + end + + def set_random + self.random = rand + end + + class << self + def sample + self.by_random.startkey(rand).first || self.by_random.first + end + + def pick_from_pool + cert = self.sample + raise RECORD_NOT_FOUND unless cert + cert.destroy + return cert + rescue RESOURCE_NOT_FOUND + retry if self.by_random.count > 0 + raise RECORD_NOT_FOUND + end + + def valid_attributes_hash + {:key => "ABCD", :cert => "A123"} + end + end + + end +end diff --git a/certs/app/views/.gitkeep b/certs/app/views/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/certs/leap_web_certs.gemspec b/certs/leap_web_certs.gemspec index 81bc042..15a45be 100644 --- a/certs/leap_web_certs.gemspec +++ b/certs/leap_web_certs.gemspec @@ -16,6 +16,5 @@ Gem::Specification.new do |s| s.test_files = Dir["test/**/*"] s.add_dependency "leap_web_core", LeapWeb::VERSION - s.add_dependency "leap_ca", '~> 0.2.0' end diff --git a/certs/lib/leap_web_certs/engine.rb b/certs/lib/leap_web_certs/engine.rb index bc0801d..3c8948a 100644 --- a/certs/lib/leap_web_certs/engine.rb +++ b/certs/lib/leap_web_certs/engine.rb @@ -1,14 +1,4 @@ require "leap_web_core" -require "leap_ca/config" -LeapCA::Config.db_name = "client_certificates" - -# couchrest model has an initializer for this - but apparently that does not work -CouchRest::Model::Base.configure do |conf| - conf.environment = Rails.env - conf.connection_config_file = File.join(Rails.root, 'config', 'couchdb.yml') -end - -require "leap_ca/cert" module LeapWebCerts class Engine < ::Rails::Engine diff --git a/certs/test/unit/cert_pool_test.rb b/certs/test/unit/cert_pool_test.rb new file mode 100644 index 0000000..06f7ce0 --- /dev/null +++ b/certs/test/unit/cert_pool_test.rb @@ -0,0 +1,52 @@ +require 'test_helper' + +class CertPoolTest < ActiveSupport::TestCase + + setup do + 2.times { LeapCA::Cert.create(LeapCA::Cert.valid_attributes_hash) } + end + + teardown do + LeapCA::Cert.all.each {|c| c.destroy} + end + + test "picks random sample" do + # with 3 certs chances are pretty low we pick the same one 40 times. + LeapCA::Cert.create! LeapCA::Cert.valid_attributes_hash + picked = [] + first = LeapCA::Cert.sample.id + current = LeapCA::Cert.sample.id + 40.times do + break if current != first + current = LeapCA::Cert.sample.id + end + assert_not_equal current, first + end + + test "picks cert from the pool" do + assert_difference "LeapCA::Cert.count", -1 do + cert = LeapCA::Cert.pick_from_pool + end + end + + test "err's out if all certs have been destroyed" do + sample = LeapCA::Cert.first.tap{|c| c.destroy} + LeapCA::Cert.all.each {|c| c.destroy} + assert_raises RECORD_NOT_FOUND do + LeapCA::Cert.expects(:sample).returns(sample) + cert = LeapCA::Cert.pick_from_pool + end + end + + test "picks other cert if first pick has been destroyed" do + first = LeapCA::Cert.first.tap{|c| c.destroy} + second = LeapCA::Cert.first + LeapCA::Cert.expects(:sample).at_least_once. + returns(first). + then.returns(second) + cert = LeapCA::Cert.pick_from_pool + assert_equal second, cert + assert_nil LeapCA::Cert.first + end + +end diff --git a/certs/test/unit/cert_test.rb b/certs/test/unit/cert_test.rb new file mode 100644 index 0000000..0b21d0b --- /dev/null +++ b/certs/test/unit/cert_test.rb @@ -0,0 +1,39 @@ +require 'test_helper' + +class CertTest < ActiveSupport::TestCase + + setup do + @sample = LeapCA::Cert.new LeapCA::Cert.valid_attributes_hash + end + + test "stub cert for testing is valid" do + assert @sample.valid? + end + + test "setting random on create validation" do + @sample.random = "asdf" + assert @sample.valid? + assert @sample.random.is_a? Float + assert @sample.random >= 0 + assert @sample.random < 1 + end + + test "validates random" do + @sample.save # make sure we are past the on_create + assert @sample.valid? + ["asdf", 1, 2, -0.1, nil, "asdf"].each do |invalid| + @sample.random = invalid + assert !@sample.valid?, "#{invalid} should not be a valid value for random" + end + end + + test "validates key" do + @sample.key = nil + assert !@sample.valid?, "Cert should require key" + end + + test "validates cert" do + @sample.cert = nil + assert !@sample.valid?, "Cert should require cert" + end +end -- cgit v1.2.3 From 2599c7bac06ee55d58e492a47e09ee163e9582ba Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 8 Jan 2013 13:20:34 -0800 Subject: Adding show view for users. --- users/app/controllers/users_controller.rb | 2 +- users/app/helpers/users_helper.rb | 6 ++++++ users/app/models/user.rb | 4 ++++ users/app/views/users/_user.html.haml | 2 +- users/app/views/users/show.html.haml | 32 +++++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 users/app/views/users/show.html.haml diff --git a/users/app/controllers/users_controller.rb b/users/app/controllers/users_controller.rb index 79de630..eb93fcb 100644 --- a/users/app/controllers/users_controller.rb +++ b/users/app/controllers/users_controller.rb @@ -2,7 +2,7 @@ class UsersController < ApplicationController skip_before_filter :verify_authenticity_token, :only => [:create] - before_filter :fetch_user, :only => [:edit, :update, :destroy] + before_filter :fetch_user, :only => [:show, :edit, :update, :destroy] before_filter :set_anchor, :only => [:edit, :update] before_filter :authorize_admin, :only => [:index] diff --git a/users/app/helpers/users_helper.rb b/users/app/helpers/users_helper.rb index 45ca0e9..5f68085 100644 --- a/users/app/helpers/users_helper.rb +++ b/users/app/helpers/users_helper.rb @@ -30,4 +30,10 @@ module UsersHelper classes.compact end + def user_field(field) + value = @user.send(field) + value = value.to_s(:long) if field.end_with? '_at' + value || 'not set' + end + end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 1798ea4..4b6b06c 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -100,6 +100,10 @@ class User < CouchRest::Model::Base email_aliases.build(attrs.values.first) if attrs end + def most_recent_tickets(count=3) + Ticket.for_user(self).limit(count) #defaults to having most recent updated first + end + protected ## diff --git a/users/app/views/users/_user.html.haml b/users/app/views/users/_user.html.haml index 7db0041..ca03d34 100644 --- a/users/app/views/users/_user.html.haml +++ b/users/app/views/users/_user.html.haml @@ -1,5 +1,5 @@ %tr - %td= user.login + %td= link_to user.login, user %td= time_ago_in_words(user.created_at) + " ago" %td = link_to edit_user_path(user), :class => "btn btn-mini btn-primary" do diff --git a/users/app/views/users/show.html.haml b/users/app/views/users/show.html.haml new file mode 100644 index 0000000..9df029e --- /dev/null +++ b/users/app/views/users/show.html.haml @@ -0,0 +1,32 @@ +.span8.offset1 + %h2= @user.login + %dl.offset1 + - fields = ['login', 'email', 'created_at', 'updated_at', 'email_forward'] + - fields.each do |field| + %dt + = field.titleize + %dd + = user_field(field) + %dt + =t :email_aliases + %dd + - aliases = @user.email_aliases + - if aliases.empty? + none set + - else + %ul.unstyled + - aliases.each do |al| + %li + = al.email + %dt + =t :most_recently_updated_tickets + %dd + %ul + - @user.most_recent_tickets.each do |ticket| + %li + = link_to ticket.title, ticket + %small + updated: + = ticket.updated_at.to_s(:long) + + -- cgit v1.2.3 From d81bf00ecd8bdfcddf50e4881428c917253326fe Mon Sep 17 00:00:00 2001 From: jessib Date: Thu, 10 Jan 2013 11:06:09 -0800 Subject: Add test for showing user. --- users/test/functional/users_controller_test.rb | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/users/test/functional/users_controller_test.rb b/users/test/functional/users_controller_test.rb index 1fa1462..1f6c868 100644 --- a/users/test/functional/users_controller_test.rb +++ b/users/test/functional/users_controller_test.rb @@ -9,12 +9,31 @@ class UsersControllerTest < ActionController::TestCase assert_response :success end + test "failed show without login" do + user = find_record User + get :show, :id => user.id + assert_response :redirect + assert_redirected_to login_path + end + + test "user can see user" do + user = find_record User, + :email => nil, + :email_forward => nil, + :email_aliases => [], + :created_at => Time.now, + :updated_at => Time.now, + :most_recent_tickets => [] + login user + get :show, :id => user.id + assert_response :success + end + test "should create new user" do user = stub_record User User.expects(:create).with(user.params).returns(user) post :create, :user => user.params, :format => :json - assert_nil session[:user_id] assert_json_response user assert_response :success -- cgit v1.2.3 From 1623f51cc003f7de92f7192bba5ca88dfb2a92f6 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 11 Jan 2013 10:23:51 +0100 Subject: basic dummy data for users --- common_dependencies.rb | 5 +++++ test/factories.rb | 3 +++ users/app/models/user.rb | 6 +++--- users/test/factories.rb | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/factories.rb create mode 100644 users/test/factories.rb diff --git a/common_dependencies.rb b/common_dependencies.rb index a6691cf..1650fee 100644 --- a/common_dependencies.rb +++ b/common_dependencies.rb @@ -4,3 +4,8 @@ group :test do gem 'mocha', '~> 0.13.0', :require => false end +group :test, :development do + gem 'faker' + gem 'factory_girl_rails' +end + diff --git a/test/factories.rb b/test/factories.rb new file mode 100644 index 0000000..6c671f8 --- /dev/null +++ b/test/factories.rb @@ -0,0 +1,3 @@ +Dir.glob(Rails.root.join('**','test','factories.rb')) do |factory_file| + require factory_file +end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 1798ea4..8474d16 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -18,8 +18,8 @@ class User < CouchRest::Model::Base :if => :serverside? validates :login, - :format => { :with => /\A[A-Za-z\d_]+\z/, - :message => "Only letters, digits and _ allowed" } + :format => { :with => /\A[A-Za-z\d_\.]+\z/, + :message => "Only letters, digits, . and _ allowed" } validates :password_salt, :password_verifier, :format => { :with => /\A[\dA-Fa-f]+\z/, :message => "Only hex numbers allowed" } @@ -57,7 +57,7 @@ class User < CouchRest::Model::Base # valid set of attributes for testing def valid_attributes_hash - { :login => "me", + { :login => Faker::Name.first_name.downcase, :password_verifier => "1234ABCD", :password_salt => "4321AB" } end diff --git a/users/test/factories.rb b/users/test/factories.rb new file mode 100644 index 0000000..2802c7e --- /dev/null +++ b/users/test/factories.rb @@ -0,0 +1,18 @@ +FactoryGirl.define do + + factory :user do + login { Faker::Internet.user_name } + password_verifier "1234ABCD" + password_salt "4321AB" + end + + factory :user_with_settings, :class => User do + login { Faker::Internet.user_name } + password_verifier "1234ABCD" + password_salt "4321AB" + email_forward { Faker::Internet.email } + email_aliases_attributes do + {:a => Faker::Internet.user_name + '@' + APP_CONFIG[:domain]} + end + end +end -- cgit v1.2.3 From 4fd47160e0c43695aada330154f41821099bdda0 Mon Sep 17 00:00:00 2001 From: elijah Date: Sun, 13 Jan 2013 12:13:03 -0800 Subject: add head and tail scss, for customization --- app/assets/stylesheets/application.scss | 17 +++++++++++++---- app/assets/stylesheets/head.scss | 3 +++ app/assets/stylesheets/tail.scss | 3 +++ 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 app/assets/stylesheets/head.scss create mode 100644 app/assets/stylesheets/tail.scss diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index cb5fa1b..bb3b8bc 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -1,16 +1,25 @@ +// +// import custom scss, content to be set in deployment. +// +@import "tail"; + +// +// import bootstrap. +// @import "bootstrap"; body { padding: 40px; } @import "bootstrap-responsive"; - - table.table-hover .btn { opacity: 0; } - table.table-hover tr:hover .btn { opacity: 1; } +@import "bootstrap-editable"; -@import "bootstrap-editable"; \ No newline at end of file +// +// import custom scss, content to be set in deployment. +// +@import "tail"; diff --git a/app/assets/stylesheets/head.scss b/app/assets/stylesheets/head.scss new file mode 100644 index 0000000..431565f --- /dev/null +++ b/app/assets/stylesheets/head.scss @@ -0,0 +1,3 @@ +// +// put custom scss here that goes before all the others +// diff --git a/app/assets/stylesheets/tail.scss b/app/assets/stylesheets/tail.scss new file mode 100644 index 0000000..541c2df --- /dev/null +++ b/app/assets/stylesheets/tail.scss @@ -0,0 +1,3 @@ +// +// put custom scss here that goes after all the others +// -- cgit v1.2.3 From 3cd3c261bdf02b2da5217fa1c469d30f9d92c9a3 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 14 Jan 2013 12:04:12 +0100 Subject: got users controller test to pass - tickets controller test next. --- users/app/models/user.rb | 12 ++----- users/test/factories.rb | 17 +++++----- users/test/functional/users_controller_test.rb | 43 +++++++++++++------------- users/test/support/stub_record_helper.rb | 32 +++++++++---------- users/test/unit/email_aliases_test.rb | 8 ++--- users/test/unit/email_test.rb | 9 ++---- users/test/unit/user_test.rb | 17 +++++----- 7 files changed, 61 insertions(+), 77 deletions(-) diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 8474d16..f20c6ac 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -54,17 +54,11 @@ class User < CouchRest::Model::Base class << self alias_method :find_by_param, :find - - # valid set of attributes for testing - def valid_attributes_hash - { :login => Faker::Name.first_name.downcase, - :password_verifier => "1234ABCD", - :password_salt => "4321AB" } - end - end - alias_method :to_param, :id + def to_param + self.id + end def to_json(options={}) { diff --git a/users/test/factories.rb b/users/test/factories.rb index 2802c7e..6b094bd 100644 --- a/users/test/factories.rb +++ b/users/test/factories.rb @@ -4,15 +4,16 @@ FactoryGirl.define do login { Faker::Internet.user_name } password_verifier "1234ABCD" password_salt "4321AB" - end - factory :user_with_settings, :class => User do - login { Faker::Internet.user_name } - password_verifier "1234ABCD" - password_salt "4321AB" - email_forward { Faker::Internet.email } - email_aliases_attributes do - {:a => Faker::Internet.user_name + '@' + APP_CONFIG[:domain]} + factory :user_with_settings do + email_forward { Faker::Internet.email } + email_aliases_attributes do + {:a => Faker::Internet.user_name + '@' + APP_CONFIG[:domain]} + end + end + + factory :admin_user do + is_admin? true end end end diff --git a/users/test/functional/users_controller_test.rb b/users/test/functional/users_controller_test.rb index 1fa1462..8f1ee15 100644 --- a/users/test/functional/users_controller_test.rb +++ b/users/test/functional/users_controller_test.rb @@ -10,10 +10,11 @@ class UsersControllerTest < ActionController::TestCase end test "should create new user" do - user = stub_record User - User.expects(:create).with(user.params).returns(user) + user_attribs = record_attributes_for :user + user = User.new(user_attribs) + User.expects(:create).with(user_attribs).returns(user) - post :create, :user => user.params, :format => :json + post :create, :user => user_attribs, :format => :json assert_nil session[:user_id] assert_json_response user @@ -21,23 +22,20 @@ class UsersControllerTest < ActionController::TestCase end test "should redirect to signup form on failed attempt" do - params = User.valid_attributes_hash.slice(:login) - user = User.new(params) - params.stringify_keys! + user_attribs = record_attributes_for :user + user_attribs.slice!('login') + user = User.new(user_attribs) assert !user.valid? - User.expects(:create).with(params).returns(user) + User.expects(:create).with(user_attribs).returns(user) - post :create, :user => params, :format => :json + post :create, :user => user_attribs, :format => :json assert_json_error user.errors.messages assert_response 422 end test "should get edit view" do - user = find_record User, - :email => nil, - :email_forward => nil, - :email_aliases => [] + user = find_record :user login user get :edit, :id => user.id @@ -46,14 +44,14 @@ class UsersControllerTest < ActionController::TestCase end test "user can change settings" do - user = find_record User - user.expects(:attributes=).with(user.params) + user = find_record :user + changed_attribs = record_attributes_for :user_with_settings + user.expects(:attributes=).with(changed_attribs) user.expects(:changed?).returns(true) user.expects(:save).returns(true) - user.stubs(:email_aliases).returns([]) login user - put :update, :user => user.params, :id => user.id, :format => :json + put :update, :user => changed_attribs, :id => user.id, :format => :json assert_equal user, assigns[:user] assert_response 204 @@ -61,14 +59,15 @@ class UsersControllerTest < ActionController::TestCase end test "admin can update user" do - user = find_record User - user.expects(:attributes=).with(user.params) + user = find_record :user + changed_attribs = record_attributes_for :user_with_settings + user.expects(:attributes=).with(changed_attribs.stringify_keys) user.expects(:changed?).returns(true) user.expects(:save).returns(true) user.stubs(:email_aliases).returns([]) login :is_admin? => true - put :update, :user => user.params, :id => user.id, :format => :json + put :update, :user => changed_attribs, :id => user.id, :format => :json assert_equal user, assigns[:user] assert_response 204 @@ -76,7 +75,7 @@ class UsersControllerTest < ActionController::TestCase end test "admin can destroy user" do - user = find_record User + user = find_record :user user.expects(:destroy) login :is_admin? => true @@ -87,7 +86,7 @@ class UsersControllerTest < ActionController::TestCase end test "user can cancel account" do - user = find_record User + user = find_record :user user.expects(:destroy) login user @@ -98,7 +97,7 @@ class UsersControllerTest < ActionController::TestCase end test "non-admin can't destroy user" do - user = stub_record User + user = find_record :user login delete :destroy, :id => user.id diff --git a/users/test/support/stub_record_helper.rb b/users/test/support/stub_record_helper.rb index 1be419a..18db5ae 100644 --- a/users/test/support/stub_record_helper.rb +++ b/users/test/support/stub_record_helper.rb @@ -4,10 +4,11 @@ module StubRecordHelper # return the record given. # If no record is given but a hash or nil will create a stub based on # that instead and returns the stub. - def find_record(klass, record_or_method_hash = {}) - record = stub_record(klass, record_or_method_hash) + def find_record(factory) + record = stub_record factory + klass = record.class finder = klass.respond_to?(:find_by_param) ? :find_by_param : :find_by_id - klass.expects(finder).with(record.to_param).returns(record) + klass.expects(finder).with(record.to_param.to_s).returns(record) return record end @@ -17,25 +18,24 @@ module StubRecordHelper # If the second parameter is a record we return the record itself. # This way you can build functions that either take a record or a # method hash to stub from. See find_record for an example. - def stub_record(klass, record_or_method_hash = {}, persisted = true) + def stub_record(factory, record_or_method_hash = {}) if record_or_method_hash && !record_or_method_hash.is_a?(Hash) return record_or_method_hash end - stub record_params_for(klass, record_or_method_hash, persisted) + FactoryGirl.build_stubbed(factory).tap do |record| + record.stubs(record_or_method_hash) if record_or_method_hash.present? + end end - def record_params_for(klass, params = {}, persisted = true) - if klass.respond_to?(:valid_attributes_hash) - params.reverse_merge!(klass.valid_attributes_hash) + # returns deep stringified attributes so they can be compared to + # what the controller receives as params + def record_attributes_for(factory, attribs_hash = nil) + FactoryGirl.attributes_for(factory, attribs_hash).tap do |attribs| + attribs.keys.each do |key| + val = attribs.delete(key) + attribs[key.to_s] = val.is_a?(Hash) ? val.stringify_keys! : val + end end - params[:params] = params.stringify_keys - params.reverse_merge! :id => "A123", - :to_param => "A123", - :class => klass, - :to_key => ['123'], - :to_json => %Q({"stub":"#{klass.name}"}), - :new_record? => !persisted, - :persisted? => persisted end end diff --git a/users/test/unit/email_aliases_test.rb b/users/test/unit/email_aliases_test.rb index 88f97f4..e3f060d 100644 --- a/users/test/unit/email_aliases_test.rb +++ b/users/test/unit/email_aliases_test.rb @@ -3,12 +3,8 @@ require 'test_helper' class EmailAliasTest < ActiveSupport::TestCase setup do - @attribs = User.valid_attributes_hash - User.find_by_login(@attribs[:login]).try(:destroy) - @user = User.new(@attribs) - @attribs.merge!(:login => "other_user") - User.find_by_login(@attribs[:login]).try(:destroy) - @other_user = User.create(@attribs) + @user = FactoryGirl.build :user + @other_user = FactoryGirl.build :user @alias = "valid_alias@#{APP_CONFIG[:domain]}" User.find_by_email_or_alias(@alias).try(:destroy) end diff --git a/users/test/unit/email_test.rb b/users/test/unit/email_test.rb index 060ced5..d7ef1f8 100644 --- a/users/test/unit/email_test.rb +++ b/users/test/unit/email_test.rb @@ -3,13 +3,8 @@ require 'test_helper' class EmailTest < ActiveSupport::TestCase setup do - # TODO build helper for this ... make_record(User) - @attribs = User.valid_attributes_hash - User.find_by_login(@attribs[:login]).try(:destroy) - @user = User.new(@attribs) - @attribs.merge!(:login => "other_user") - User.find_by_login(@attribs[:login]).try(:destroy) - @other_user = User.create(@attribs) + @user = FactoryGirl.build :user + @other_user = FactoryGirl.build :user @email_string = "valid_alias@#{APP_CONFIG[:domain]}" User.find_by_email_or_alias(@email_string).try(:destroy) end diff --git a/users/test/unit/user_test.rb b/users/test/unit/user_test.rb index 0c79f1f..917728b 100644 --- a/users/test/unit/user_test.rb +++ b/users/test/unit/user_test.rb @@ -4,9 +4,7 @@ class UserTest < ActiveSupport::TestCase include SRP::Util setup do - @attribs = User.valid_attributes_hash - User.find_by_login(@attribs[:login]).try(:destroy) - @user = User.new(@attribs) + @user = FactoryGirl.build(:user) end test "test set of attributes should be valid" do @@ -49,13 +47,14 @@ class UserTest < ActiveSupport::TestCase assert_equal client_rnd, srp_session.aa end - test 'is user an admin' do - admin_login = APP_CONFIG['admins'].first - attribs = User.valid_attributes_hash - attribs[:login] = admin_login - admin_user = User.new(attribs) - assert admin_user.is_admin? + test 'normal user is no admin' do assert !@user.is_admin? end + test 'user with login in APP_CONFIG is an admin' do + admin_login = APP_CONFIG['admins'].first + @user.login = admin_login + assert @user.is_admin? + end + end -- cgit v1.2.3 From a9ef046a596b5876792b1cc4ab0d26b9b255c342 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 14 Jan 2013 16:24:25 +0100 Subject: tickets controller tests passing --- help/app/views/tickets/_ticket_data.html.haml | 2 +- help/test/functional/tickets_controller_test.rb | 69 +++++++++++++------------ users/test/factories.rb | 1 + users/test/support/auth_test_helper.rb | 6 +-- users/test/support/stub_record_helper.rb | 13 +++-- 5 files changed, 49 insertions(+), 42 deletions(-) diff --git a/help/app/views/tickets/_ticket_data.html.haml b/help/app/views/tickets/_ticket_data.html.haml index 3d301be..fee080f 100644 --- a/help/app/views/tickets/_ticket_data.html.haml +++ b/help/app/views/tickets/_ticket_data.html.haml @@ -22,4 +22,4 @@ = button_to 'Close', {:post => {:is_open => false}}, :method => :put, :class => 'btn btn-small' - else = 'closed' - = button_to 'Open', {:post => {:is_open => true}}, :method => :put, :class => 'btn btn-small' \ No newline at end of file + = button_to 'Open', {:post => {:is_open => true}}, :method => :put, :class => 'btn btn-small' diff --git a/help/test/functional/tickets_controller_test.rb b/help/test/functional/tickets_controller_test.rb index 7060936..97e3308 100644 --- a/help/test/functional/tickets_controller_test.rb +++ b/help/test/functional/tickets_controller_test.rb @@ -3,21 +3,17 @@ require 'test_helper' class TicketsControllerTest < ActionController::TestCase setup do - User.create(User.valid_attributes_hash.merge({:login => 'first_test'})) - User.create(User.valid_attributes_hash.merge({:login => 'different'})) - Ticket.create( {:title => "stub test ticket", :id => 'stubtestticketid', :comments_attributes => {"0" => {"body" =>"body of stubbed test ticket"}}}) - Ticket.create( {:title => "stub test ticket two", :id => 'stubtestticketid2', :comments_attributes => {"0" => {"body" =>"body of second stubbed test ticket"}}}) + @user = FactoryGirl.create :user + @other_user = FactoryGirl.create :user end teardown do - User.find_by_login('first_test').destroy - User.find_by_login('different').destroy - Ticket.find('stubtestticketid').destroy - Ticket.find('stubtestticketid2').destroy + @user.destroy + @other_user.destroy end test "should get index if logged in" do - login :is_admin? => false + login get :index assert_response :success assert_not_nil assigns(:tickets) @@ -35,29 +31,32 @@ class TicketsControllerTest < ActionController::TestCase assert_response :success end - test "ticket show access" do - ticket = Ticket.first - ticket.created_by = nil # TODO: hacky, but this makes sure this ticket is an unauthenticated one - ticket.save + test "unauthenticated tickets are visible" do + ticket = find_record :ticket, :created_by => nil get :show, :id => ticket.id assert_response :success + end - ticket.created_by = User.last.id - ticket.save + test "user tickets are not visible without login" do + ticket = find_record :ticket, :created_by => @user.id get :show, :id => ticket.id assert_response :redirect assert_redirected_to login_url + end - login(User.last) + test "user tickets are visible to creator" do + ticket = find_record :ticket, :created_by => @user.id + login @user get :show, :id => ticket.id assert_response :success + end - login(User.first) #assumes User.first != User.last: - assert_not_equal User.first, User.last + test "user tickets are not visible to other user" do + ticket = find_record :ticket, :created_by => @user.id + login @other_user get :show, :id => ticket.id assert_response :redirect assert_redirected_to root_url - end test "should create unauthenticated ticket" do @@ -80,7 +79,7 @@ class TicketsControllerTest < ActionController::TestCase params = {:title => "auth ticket test title", :comments_attributes => {"0" => {"body" =>"body of test ticket"}}} - login :email => "test@email.net" + login :email => Faker::Internet.user_name + '@' + APP_CONFIG[:domain] assert_difference('Ticket.count') do post :create, :ticket => params @@ -237,30 +236,32 @@ class TicketsControllerTest < ActionController::TestCase end test "tickets for regular user" do - login :is_admin? => false, :email => nil + login + ticket = FactoryGirl.create :ticket + other_ticket = FactoryGirl.create :ticket - put :update, :id => 'stubtestticketid',:ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} } + put :update, :id => ticket.id, + :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} } assert_not_nil assigns(:ticket).comments.last.posted_by assert_equal assigns(:ticket).comments.last.posted_by, @current_user.id get :index, {:open_status => "open"} assert assigns(:all_tickets).count > 0 - assert assigns(:all_tickets).include?(Ticket.find('stubtestticketid')) - - assert !assigns(:all_tickets).include?(Ticket.find('stubtestticketid2')) + assert assigns(:all_tickets).include?(ticket) + assert !assigns(:all_tickets).include?(other_ticket) # user should have one more ticket if a new tick gets a comment by this user assert_difference('assigns[:all_tickets].count') do - put :update, :id => 'stubtestticketid2' , :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}}} + put :update, :id => other_ticket.id, :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}}} get :index, {:open_status => "open"} end - assert assigns(:all_tickets).include?(Ticket.find('stubtestticketid2')) + assert assigns(:all_tickets).include?(other_ticket) # if we close one ticket, the user should have 1 less open ticket assert_difference('assigns[:all_tickets].count', -1) do - t = Ticket.find('stubtestticketid2') - t.close - t.save + other_ticket.reload + other_ticket.close + other_ticket.save get :index, {:open_status => "open"} end @@ -268,14 +269,14 @@ class TicketsControllerTest < ActionController::TestCase # look at closed tickets: get :index, {:open_status => "closed"} - assert assigns(:all_tickets).include?(Ticket.find('stubtestticketid2')) - assert !assigns(:all_tickets).include?(Ticket.find('stubtestticketid')) + assert !assigns(:all_tickets).include?(ticket) + assert assigns(:all_tickets).include?(other_ticket) number_closed_tickets = assigns(:all_tickets).count # all tickets should equal closed + open get :index, {:open_status => "all"} - assert assigns(:all_tickets).include?(Ticket.find('stubtestticketid2')) - assert assigns(:all_tickets).include?(Ticket.find('stubtestticketid')) + assert assigns(:all_tickets).include?(ticket) + assert assigns(:all_tickets).include?(other_ticket) assert_equal assigns(:all_tickets).count, number_closed_tickets + number_open_tickets end diff --git a/users/test/factories.rb b/users/test/factories.rb index 6b094bd..4bf7e62 100644 --- a/users/test/factories.rb +++ b/users/test/factories.rb @@ -7,6 +7,7 @@ FactoryGirl.define do factory :user_with_settings do email_forward { Faker::Internet.email } + email { Faker::Internet.user_name + '@' + APP_CONFIG[:domain] } email_aliases_attributes do {:a => Faker::Internet.user_name + '@' + APP_CONFIG[:domain]} end diff --git a/users/test/support/auth_test_helper.rb b/users/test/support/auth_test_helper.rb index c9f5612..c0fcf3a 100644 --- a/users/test/support/auth_test_helper.rb +++ b/users/test/support/auth_test_helper.rb @@ -10,10 +10,10 @@ module AuthTestHelper end def login(user_or_method_hash = {}) - @current_user = stub_record(User, user_or_method_hash) - unless @current_user.respond_to? :is_admin? - @current_user.stubs(:is_admin?).returns(false) + if user_or_method_hash.respond_to?(:reverse_merge) + user_or_method_hash.reverse_merge! :is_admin? => false end + @current_user = stub_record(:user, user_or_method_hash, true) request.env['warden'] = stub :user => @current_user return @current_user end diff --git a/users/test/support/stub_record_helper.rb b/users/test/support/stub_record_helper.rb index 18db5ae..168a827 100644 --- a/users/test/support/stub_record_helper.rb +++ b/users/test/support/stub_record_helper.rb @@ -4,10 +4,11 @@ module StubRecordHelper # return the record given. # If no record is given but a hash or nil will create a stub based on # that instead and returns the stub. - def find_record(factory) - record = stub_record factory + def find_record(factory, attribs_hash = {}) + attribs_hash.reverse_merge!(:id => Random.rand(10000).to_s) + record = stub_record factory, attribs_hash klass = record.class - finder = klass.respond_to?(:find_by_param) ? :find_by_param : :find_by_id + finder = klass.respond_to?(:find_by_param) ? :find_by_param : :find klass.expects(finder).with(record.to_param.to_s).returns(record) return record end @@ -18,11 +19,15 @@ module StubRecordHelper # If the second parameter is a record we return the record itself. # This way you can build functions that either take a record or a # method hash to stub from. See find_record for an example. - def stub_record(factory, record_or_method_hash = {}) + def stub_record(factory, record_or_method_hash = {}, persisted=false) if record_or_method_hash && !record_or_method_hash.is_a?(Hash) return record_or_method_hash end FactoryGirl.build_stubbed(factory).tap do |record| + if persisted or record.persisted? + record_or_method_hash.reverse_merge! :created_at => Time.now, + :updated_at => Time.now, :id => Random.rand(100000).to_s + end record.stubs(record_or_method_hash) if record_or_method_hash.present? end end -- cgit v1.2.3 From 632a31cf48f41b3d0ea52f3b741407d65ab64139 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 14 Jan 2013 16:30:32 +0100 Subject: forgot to include the factory for tickets --- help/test/factories.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 help/test/factories.rb diff --git a/help/test/factories.rb b/help/test/factories.rb new file mode 100644 index 0000000..919882a --- /dev/null +++ b/help/test/factories.rb @@ -0,0 +1,10 @@ +FactoryGirl.define do + + factory :ticket do + title { Faker::Lorem.sentence } + comments_attributes do + { "0" => { "body" => Faker::Lorem.sentences } } + end + end + +end -- cgit v1.2.3 From 581bcc0507b2f2385bf1f6d82bb119afa2f00716 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 14 Jan 2013 16:59:55 +0100 Subject: little bit of cleanup * checking records are actually getting created / destroyed * simplify creation where possible --- help/test/functional/tickets_controller_test.rb | 39 +++++++++++-------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/help/test/functional/tickets_controller_test.rb b/help/test/functional/tickets_controller_test.rb index 97e3308..794b119 100644 --- a/help/test/functional/tickets_controller_test.rb +++ b/help/test/functional/tickets_controller_test.rb @@ -98,11 +98,9 @@ class TicketsControllerTest < ActionController::TestCase end test "add comment to unauthenticated ticket" do - ticket = Ticket.find('stubtestticketid') - ticket.created_by = nil # TODO: hacky, but this makes sure this ticket is an unauthenticated one - ticket.save + ticket = FactoryGirl.create :ticket, :created_by => nil - assert_difference('Ticket.find("stubtestticketid").comments.count') do + assert_difference('Ticket.find(ticket.id).comments.count') do put :update, :id => ticket.id, :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} } end @@ -110,24 +108,24 @@ class TicketsControllerTest < ActionController::TestCase assert_equal ticket, assigns(:ticket) # still same ticket, with different comments assert_not_equal ticket.comments, assigns(:ticket).comments # ticket == assigns(:ticket), but they have different comments (which we want) + assigns(:ticket).destroy end test "add comment to own authenticated ticket" do login User.last - ticket = Ticket.find('stubtestticketid') - ticket.created_by = @current_user.id # TODO: hacky, but confirms it is their ticket - ticket.save + ticket = FactoryGirl.create :ticket, :created_by => @current_user.id #they should be able to comment if it is their ticket: - assert_difference('Ticket.find("stubtestticketid").comments.count') do + assert_difference('Ticket.find(ticket.id).comments.count') do put :update, :id => ticket.id, :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} } end assert_not_equal ticket.comments, assigns(:ticket).comments assert_not_nil assigns(:ticket).comments.last.posted_by assert_equal assigns(:ticket).comments.last.posted_by, @current_user.id + assigns(:ticket).destroy end @@ -135,17 +133,13 @@ class TicketsControllerTest < ActionController::TestCase test "cannot comment if it is not your ticket" do login :is_admin? => false, :email => nil - ticket = Ticket.first - - assert_not_nil User.first.id - ticket.created_by = User.first.id - ticket.save + ticket = FactoryGirl.create :ticket, :created_by => @other_user.id # they should *not* be able to comment if it is not their ticket put :update, :id => ticket.id, :ticket => {:comments_attributes => {"0" => {"body" =>"not allowed comment"}} } assert_response :redirect assert_access_denied - assert_equal ticket.comments, assigns(:ticket).comments + assert_equal ticket.comments.map(&:body), assigns(:ticket).comments.map(&:body) end @@ -154,14 +148,10 @@ class TicketsControllerTest < ActionController::TestCase login :is_admin? => true - ticket = Ticket.find('stubtestticketid') - assert_not_nil User.last.id - ticket.created_by = User.last.id # TODO: hacky, but confirms it somebody elses ticket: - assert_not_equal User.last.id, @current_user.id - ticket.save + ticket = FactoryGirl.create :ticket, :created_by => @other_user.id #admin should be able to comment: - assert_difference('Ticket.find("stubtestticketid").comments.count') do + assert_difference('Ticket.find(ticket.id).comments.count') do put :update, :id => ticket.id, :ticket => {:comments_attributes => {"0" => {"body" =>"NEWER comment"}} } end @@ -169,9 +159,11 @@ class TicketsControllerTest < ActionController::TestCase assert_not_nil assigns(:ticket).comments.last.posted_by assert_equal assigns(:ticket).comments.last.posted_by, @current_user.id + assigns(:ticket).destroy end test "tickets by admin" do + ticket = FactoryGirl.create :ticket, :created_by => @other_user.id login :is_admin? => true, :email => nil @@ -188,17 +180,18 @@ class TicketsControllerTest < ActionController::TestCase test "admin_status mine vs all" do - testticket = Ticket.create :title => 'temp testytest' + testticket = FactoryGirl.create :ticket login :is_admin? => true, :email => nil get :index, {:admin_status => "all", :open_status => "open"} assert assigns(:all_tickets).include?(testticket) get :index, {:admin_status => "mine", :open_status => "open"} assert !assigns(:all_tickets).include?(testticket) + testticket.destroy end test "commenting on a ticket adds to tickets that are mine" do - testticket = Ticket.create :title => 'temp testytest' + testticket = FactoryGirl.create :ticket login :is_admin? => true, :email => nil get :index, {:admin_status => "mine", :open_status => "open"} @@ -215,6 +208,7 @@ class TicketsControllerTest < ActionController::TestCase end test "admin ticket ordering" do + tickets = FactoryGirl.create_list :ticket, 2 login :is_admin? => true, :email => nil get :index, {:admin_status => "all", :open_status => "open", :sort_order => 'created_at_desc'} @@ -233,6 +227,7 @@ class TicketsControllerTest < ActionController::TestCase assert_not_equal first_tick, assigns(:all_tickets).first assert_not_equal last_tick, assigns(:all_tickets).last + tickets.each {|ticket| ticket.destroy} end test "tickets for regular user" do -- cgit v1.2.3 From 7b323c5dbd7171cf98f69ca159b5aa2174a5d069 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 14 Jan 2013 20:02:17 +0100 Subject: minor: further cleanup - try to leave no record behind --- help/test/factories.rb | 2 +- help/test/functional/tickets_controller_test.rb | 2 ++ help/test/unit/ticket_comment_test.rb | 5 +++-- help/test/unit/ticket_test.rb | 1 + users/test/integration/api/account_flow_test.rb | 7 ++----- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/help/test/factories.rb b/help/test/factories.rb index 919882a..5b38952 100644 --- a/help/test/factories.rb +++ b/help/test/factories.rb @@ -3,7 +3,7 @@ FactoryGirl.define do factory :ticket do title { Faker::Lorem.sentence } comments_attributes do - { "0" => { "body" => Faker::Lorem.sentences } } + { "0" => { "body" => Faker::Lorem.sentences.join(" ") } } end end diff --git a/help/test/functional/tickets_controller_test.rb b/help/test/functional/tickets_controller_test.rb index 794b119..0c462a9 100644 --- a/help/test/functional/tickets_controller_test.rb +++ b/help/test/functional/tickets_controller_test.rb @@ -274,6 +274,8 @@ class TicketsControllerTest < ActionController::TestCase assert assigns(:all_tickets).include?(other_ticket) assert_equal assigns(:all_tickets).count, number_closed_tickets + number_open_tickets + assigns(:all_tickets).each {|t| t.destroy} + end end diff --git a/help/test/unit/ticket_comment_test.rb b/help/test/unit/ticket_comment_test.rb index 1fe1fe2..44865ed 100644 --- a/help/test/unit/ticket_comment_test.rb +++ b/help/test/unit/ticket_comment_test.rb @@ -21,11 +21,11 @@ class TicketCommentTest < ActiveSupport::TestCase #comment.ticket = testticket #Ticket.find_by_title("testing") #assert_equal testticket.title, comment.ticket.title - + #tc.ticket = Ticket.find_by_title("test title") #tc.ticket.title end - + =begin test "create authenticated comment" do User.current = 4 @@ -49,6 +49,7 @@ class TicketCommentTest < ActiveSupport::TestCase testticket.comments << comment2 #this should validate comment2 testticket.valid? assert_equal testticket.comments.count, 2 + testticket.reload.destroy # where should posted_at be set? #assert_not_nil comment.posted_at #assert_not_nil testticket.comments.last.posted_at diff --git a/help/test/unit/ticket_test.rb b/help/test/unit/ticket_test.rb index ac6426e..ce35e1d 100644 --- a/help/test/unit/ticket_test.rb +++ b/help/test/unit/ticket_test.rb @@ -32,6 +32,7 @@ class TicketTest < ActiveSupport::TestCase #p t.email_address #p t.email_address.strip =~ RFC822::EmailAddress assert !t.valid? + t.reload.destroy end test "creation validated" do diff --git a/users/test/integration/api/account_flow_test.rb b/users/test/integration/api/account_flow_test.rb index 7636f2b..b9e2a4e 100644 --- a/users/test/integration/api/account_flow_test.rb +++ b/users/test/integration/api/account_flow_test.rb @@ -12,10 +12,6 @@ class AccountFlowTest < ActiveSupport::TestCase OUTER_APP end - def teardown - Warden.test_reset! - end - def setup @login = "integration_test_user" User.find_by_login(@login).tap{|u| u.destroy if u} @@ -31,7 +27,8 @@ class AccountFlowTest < ActiveSupport::TestCase end def teardown - @user.destroy if @user # make sure we can run this test again + @user.destroy if @user + Warden.test_reset! end # this test wraps the api and implements the interface the ruby-srp client. -- cgit v1.2.3 From 8141876126aa25d713cf4b2c76c3ecff837c4ba7 Mon Sep 17 00:00:00 2001 From: jessib Date: Mon, 14 Jan 2013 12:39:59 -0800 Subject: Use partials for displaying details shown when viewing a user. Some of these partials have specific CSS for another use, so we will likely want to tweak this. --- help/app/views/tickets/_ticket.html.haml | 5 +++-- users/app/models/user.rb | 2 +- users/app/views/emails/_email.html.haml | 5 +++-- users/app/views/users/show.html.haml | 21 ++++++++------------- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/help/app/views/tickets/_ticket.html.haml b/help/app/views/tickets/_ticket.html.haml index 3edfa8b..e02aaeb 100644 --- a/help/app/views/tickets/_ticket.html.haml +++ b/help/app/views/tickets/_ticket.html.haml @@ -1,3 +1,4 @@ += # TODO---this is now used in 2 places, and not sure we want the same CSS in both places %tr %td %b @@ -5,9 +6,9 @@ %br %small created: - = ticket.created_at.to_s(:short) + = ticket.created_at.to_s(:short) #todo doesn't show year updated: - = ticket.updated_at.to_s(:short) + = ticket.updated_at.to_s(:short) # doesn't show year %small.pull-right comments by: = ticket.commenters diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 42900ea..1e8ee0e 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -95,7 +95,7 @@ class User < CouchRest::Model::Base end def most_recent_tickets(count=3) - Ticket.for_user(self).limit(count) #defaults to having most recent updated first + Ticket.for_user(self).limit(count).all #defaults to having most recent updated first end protected diff --git a/users/app/views/emails/_email.html.haml b/users/app/views/emails/_email.html.haml index 3feb6f0..948d847 100644 --- a/users/app/views/emails/_email.html.haml +++ b/users/app/views/emails/_email.html.haml @@ -1,6 +1,7 @@ - if email.valid? %li.pull-right %code= email - = link_to(user_email_alias_path(@user, email), :method => :delete) do - %i.icon-remove + - if params[:action] == 'edit' + = link_to(user_email_alias_path(@user, email), :method => :delete) do + %i.icon-remove .clearfix diff --git a/users/app/views/users/show.html.haml b/users/app/views/users/show.html.haml index 9df029e..2b59c66 100644 --- a/users/app/views/users/show.html.haml +++ b/users/app/views/users/show.html.haml @@ -1,5 +1,7 @@ .span8.offset1 %h2= @user.login + .small + = link_to 'edit', edit_user_path(@user) %dl.offset1 - fields = ['login', 'email', 'created_at', 'updated_at', 'email_forward'] - fields.each do |field| @@ -14,19 +16,12 @@ - if aliases.empty? none set - else - %ul.unstyled - - aliases.each do |al| - %li - = al.email + .pull-left + = render aliases + .clearfix %dt =t :most_recently_updated_tickets %dd - %ul - - @user.most_recent_tickets.each do |ticket| - %li - = link_to ticket.title, ticket - %small - updated: - = ticket.updated_at.to_s(:long) - - + %table + %tbody + = render @user.most_recent_tickets \ No newline at end of file -- cgit v1.2.3 From e7d36df945792b292732e25e879a90577050a6c1 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 15 Jan 2013 11:06:46 +0100 Subject: minor: put emails in unstyled ul and simplify Just found out that render(@collection) returns nil for emtpy collections. So that is usefull for putting messages about the emtpy collection in an or clause. --- users/app/views/users/show.html.haml | 10 +++------- users/config/locales/en.yml | 1 + 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/users/app/views/users/show.html.haml b/users/app/views/users/show.html.haml index 2b59c66..d8b51e9 100644 --- a/users/app/views/users/show.html.haml +++ b/users/app/views/users/show.html.haml @@ -12,16 +12,12 @@ %dt =t :email_aliases %dd - - aliases = @user.email_aliases - - if aliases.empty? - none set - - else - .pull-left - = render aliases + %ul.pull-left.unstyled + = render(@user.email_aliases) || t(:none_set) .clearfix %dt =t :most_recently_updated_tickets %dd %table %tbody - = render @user.most_recent_tickets \ No newline at end of file + = render @user.most_recent_tickets diff --git a/users/config/locales/en.yml b/users/config/locales/en.yml index 3c71e7e..7aa23f1 100644 --- a/users/config/locales/en.yml +++ b/users/config/locales/en.yml @@ -1,4 +1,5 @@ en: + none_set: "None set." signup: "Sign up" signup_message: "Please create an account." cancel: "Cancel" -- cgit v1.2.3 From be8ee9fa669bc5554796be1fc99867fc99ba21bc Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 15 Jan 2013 11:28:37 +0100 Subject: reverted simplification - not good to have 'none set' in a %ul --- users/app/views/users/show.html.haml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/users/app/views/users/show.html.haml b/users/app/views/users/show.html.haml index d8b51e9..ec5cea6 100644 --- a/users/app/views/users/show.html.haml +++ b/users/app/views/users/show.html.haml @@ -12,8 +12,12 @@ %dt =t :email_aliases %dd - %ul.pull-left.unstyled - = render(@user.email_aliases) || t(:none_set) + - aliases = @user.email_aliases + - if aliases.present? + %ul.pull-left.unstyled + = render aliases + - else + =t :none_set .clearfix %dt =t :most_recently_updated_tickets -- cgit v1.2.3 From 9d53f7b2d1b34da6b6103e97bd6c931cedb23e9b Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 15 Jan 2013 11:03:02 -0800 Subject: Show different ticket characteristics when viewing the users versus when listing the tickets. Give a message if a user has no tickets. --- help/app/views/tickets/_ticket.html.haml | 23 +++++++++++++---------- users/app/views/users/show.html.haml | 12 ++++++++---- users/config/locales/en.yml | 2 +- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/help/app/views/tickets/_ticket.html.haml b/help/app/views/tickets/_ticket.html.haml index e02aaeb..7b37652 100644 --- a/help/app/views/tickets/_ticket.html.haml +++ b/help/app/views/tickets/_ticket.html.haml @@ -1,14 +1,17 @@ -= # TODO---this is now used in 2 places, and not sure we want the same CSS in both places +- updated_at_text = 'updated: ' + ticket.updated_at.to_s(:long) %tr %td %b = link_to ticket.title, ticket - %br - %small - created: - = ticket.created_at.to_s(:short) #todo doesn't show year - updated: - = ticket.updated_at.to_s(:short) # doesn't show year - %small.pull-right - comments by: - = ticket.commenters + - if params[:controller] == 'tickets' + %br + %small + created: + = ticket.created_at.to_s(:long) + = updated_at_text + %small.pull-right + comments by: + = ticket.commenters + - else + %small + = updated_at_text \ No newline at end of file diff --git a/users/app/views/users/show.html.haml b/users/app/views/users/show.html.haml index ec5cea6..a1eeccb 100644 --- a/users/app/views/users/show.html.haml +++ b/users/app/views/users/show.html.haml @@ -17,11 +17,15 @@ %ul.pull-left.unstyled = render aliases - else - =t :none_set + =t :none .clearfix %dt =t :most_recently_updated_tickets %dd - %table - %tbody - = render @user.most_recent_tickets + - tix = @user.most_recent_tickets + - if tix.present? + %table + %tbody + = render @user.most_recent_tickets + - else + =t :none \ No newline at end of file diff --git a/users/config/locales/en.yml b/users/config/locales/en.yml index 7aa23f1..7a6ab90 100644 --- a/users/config/locales/en.yml +++ b/users/config/locales/en.yml @@ -1,5 +1,5 @@ en: - none_set: "None set." + none: "None." signup: "Sign up" signup_message: "Please create an account." cancel: "Cancel" -- cgit v1.2.3