From 5b5431de1d72554b476b9bb81d3c7718aa581677 Mon Sep 17 00:00:00 2001 From: Carol Nichols Date: Sun, 8 Apr 2012 20:53:55 -0400 Subject: [PATCH] Add back the functionality of saving a new Author instance if the Salmon message is verified --- app/models/salmon_interpreter.rb | 21 ++++++++++++++++---- test/models/salmon_interpreter_test.rb | 27 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/app/models/salmon_interpreter.rb b/app/models/salmon_interpreter.rb index 1fc19de1..c9d4d3e5 100644 --- a/app/models/salmon_interpreter.rb +++ b/app/models/salmon_interpreter.rb @@ -14,7 +14,13 @@ def interpret # We can ignore salmon for authors that have a local user account. return true if local_user? + @author = find_or_initialize_author + raise RstatUs::InvalidSalmonMessage unless message_verified? + + if @author.new? + @author.save! + end end # Isolating calls to external classes so we can stub these methods in test @@ -29,13 +35,20 @@ def self.parse(body) OStatus::Salmon.from_xml body end - def self.find_or_create_author - end - private + def find_or_initialize_author + author = Author.first :remote_url => author_uri + + # This author is unknown to us, so let's create a new Author + unless author + end + + author + end + def message_verified? - @salmon.verified?(public_key) + @salmon.verified?(@author.retrieve_public_key) end def local_user? diff --git a/test/models/salmon_interpreter_test.rb b/test/models/salmon_interpreter_test.rb index a6459090..54888729 100644 --- a/test/models/salmon_interpreter_test.rb +++ b/test/models/salmon_interpreter_test.rb @@ -68,12 +68,39 @@ class InvalidSalmonMessage < StandardError; end end it "raises an exception if we can't verify the salmon message" do + @s.stubs(:find_or_initialize_author) @s.expects(:message_verified?).returns(false) lambda { @s.interpret }.must_raise RstatUs::InvalidSalmonMessage end + + describe "unseen" do + it "saves the new Author if the message is verified" do + author = mock + author.stubs(:new?).returns(true) + @s.expects(:find_or_initialize_author).returns(author) + @s.expects(:message_verified?).returns(true) + + author.expects(:save!) + + @s.interpret + end + + it "doesn't save the new Author if the message fails verification" do + author = mock + author.stubs(:new?).returns(true) + @s.expects(:find_or_initialize_author).returns(author) + @s.expects(:message_verified?).returns(false) + + author.expects(:save!).never + + lambda { + @s.interpret + }.must_raise RstatUs::InvalidSalmonMessage + end + end end end end \ No newline at end of file