Test for render_without_signature

This commit is contained in:
Netscape Navigator 2019-10-21 20:35:48 -05:00
parent cb13fbe57a
commit d71f1d1498
4 changed files with 73 additions and 68 deletions

101
dist/pigeon/message.rb vendored
View File

@ -13,6 +13,13 @@ module Pigeon
:timestamp, # Maybe not?
:signature # Maybe not?
def self.create(kind:, prev: nil, body: {})
self.new(author: KeyPair.current.public_key,
kind: kind,
prev: prev,
body: body).save
end
def initialize(author:,
kind:,
prev: nil,
@ -25,65 +32,15 @@ module Pigeon
@timestamp = timestamp
end
def self.create(kind:, prev: nil, body: {})
self.new(author: KeyPair.current.public_key,
kind: kind,
prev: prev,
body: body).save
end
def self.current
# TODO: Handle find-or-create logic.
@current ||= Marshal.load(Pigeon::Storage.current.get_config(NAME_OF_DRAFT))
end
def calculate_signature
string = Template.new(self).render_without_signature
KeyPair.current.sign(string)
end
def path_to_message_number(n)
File.join(".pigeon", "user", "#{n.to_s.rjust(7, "0")}.pigeon")
end
def previous_message
raise "Could not load @depth" unless @depth
if (@depth == 1)
return nil
else
Marshal.load(File.read(path_to_message_number(@depth - 1)))
end
end
def calculate_depth
Dir[OUTBOX_PATH].count
end
def sign
# Set @depth
@depth = calculate_depth
@prev = previous_message ? previous_message.signature : "".inspect
@signature = calculate_signature
file_path = path_to_message_number(@depth)
self.freeze
File.write(file_path, Marshal.dump(self))
self
end
def save
Pigeon::Storage.current.set_config(NAME_OF_DRAFT, Marshal.dump(self))
self
end
def render
Template.new(self).render
end
def message_id # I need this to calculate `prev`.
raise "NO!" unless @signature && !@signature.downcase.include?("draft")
Digest::SHA256.digest(self.render)
end
def append(key, value)
# TODO: Sanitize, validate inputs.
case value[0]
@ -95,5 +52,49 @@ module Pigeon
self.save
return self.body[key]
end
def sign
# Set @depth
@depth = calculate_depth
@prev = previous_message ? previous_message.signature : "NONE"
@signature = calculate_signature
file_path = path_to_message_number(@depth)
self.freeze
File.write(file_path, Marshal.dump(self))
self
end
def save
Pigeon::Storage.current.set_config(NAME_OF_DRAFT, Marshal.dump(self))
self
end
private
def calculate_signature
string = Template.new(self).render_without_signature
KeyPair.current.sign(string)
end
def path_to_message_number(n)
File.join(".pigeon", "user", "#{n.to_s.rjust(7, "0")}.pigeon")
end
def previous_message
raise "Could not load @depth" unless @depth
return @previous_message if @previous_message
return @previous_message = nil if @depth == 1
path = path_to_message_number(@depth - 1)
return @previous_message = Marshal.load(File.read(path))
end
def calculate_depth
Dir[OUTBOX_PATH].count
end
def message_id # I need this to calculate `prev`.
raise "NO!" unless @signature && !@signature.downcase.include?("draft")
Digest::SHA256.digest(self.render)
end
end
end

View File

@ -32,11 +32,11 @@ module Pigeon
author = message.author
body = message.body
kind = message.kind
depth = message.depth || "DRAFT"
prev = message.prev || "DRAFT"
signature = message.signature || "DRAFT"
depth = message.depth || "NONE"
prev = message.prev || "NONE"
signature = message.signature || "NONE"
ERB.new(COMPLETE_TPL).result(binding)
ERB.new(template).result(binding)
end
end
end

View File

@ -68,9 +68,11 @@ echo "== sign (publish, save, commit, etc) draft message"
echo "=== add a second message to the db"
./pigeon-cli message create second_test
echo "=== append hello:'world' to message:"
./pigeon-cli message append hello "world"
./pigeon-cli message sign
echo "=== Sign message #2"
./pigeon-cli message show
echo "getting status:"
echo "=== getting status:"
./pigeon-cli status

View File

@ -2,22 +2,24 @@ require "spec_helper"
RSpec.describe Pigeon::Template do
MessageShim = Struct.new(:author, :body, :kind, :depth, :prev, :signature)
EXPECTED_DRAFT = [
"author FAKE_AUTHOR",
"\nkind FAKE_KIND",
"\nprev DRAFT",
"\ndepth DRAFT",
"\n\n\nsignature DRAFT \n",
].join("")
it "renders a DRAFT" do
TOP_HALF = ["author FAKE_AUTHOR",
"\nkind FAKE_KIND",
"\nprev NONE",
"\ndepth 23",
"\n\nfoo:\"bar\"\n\n"].join("")
BOTTOM_HALF = "signature XYZ.sig.sha256 \n"
EXPECTED_DRAFT = TOP_HALF + BOTTOM_HALF
it "renders a draft" do
args = ["FAKE_AUTHOR",
nil,
{ foo: "bar".inspect },
"FAKE_KIND",
23,
nil,
nil,
nil]
"XYZ.sig.sha256"]
message = MessageShim.new(*args)
result = Pigeon::Template.new(message).render
expect(result).to eq(EXPECTED_DRAFT)
template = Pigeon::Template.new(message)
expect(template.render).to eq(EXPECTED_DRAFT)
expect(template.render_without_signature).to eq(TOP_HALF)
end
end