TODO: Tests for Message#sign

This commit is contained in:
Netscape Navigator 2019-12-04 22:11:04 -06:00
parent 7d8051b7b5
commit 77a5ba27de
5 changed files with 43 additions and 15 deletions

BIN
db.pigeon

Binary file not shown.

2
dist/pigeon.rb vendored
View File

@ -27,6 +27,8 @@ module Pigeon
PEER_NS = "peers"
USER_NS = "user"
BLCK_NS = "blocked"
MESG_NS = "messages"
# ^ Internal namespaces for PStore keys
BLOB_SIGIL = "&"

View File

@ -42,6 +42,10 @@ module Pigeon
@current ||= (Pigeon::Storage.current.get_config(NAME_OF_DRAFT) || new)
end
def self.reset_current
@current = nil
end
def save
Pigeon::Storage.current.set_config(NAME_OF_DRAFT, self)
self
@ -49,11 +53,10 @@ module Pigeon
def sign
@signature = calculate_signature
file_path = path_to_message_number(@depth)
self.freeze
File.write(file_path, Marshal.dump(self))
Pigeon::Storage.current.delete_config(NAME_OF_DRAFT)
self
Pigeon::Storage.current.save_message(self)
Pigeon::Message.reset_current
@signature
end
def render
@ -63,13 +66,9 @@ module Pigeon
private
def calculate_signature
# template = Template.new(self)
# string = template.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")
template = Template.new(self)
string = template.render_without_signature
KeyPair.current.sign(string)
end
def previous_message

View File

@ -6,6 +6,13 @@ module Pigeon
@current ||= self.new
end
def save_message(msg)
store.transaction do
store[MESG_NS] ||= {}
store[MESG_NS][msg.signature] = msg
end
end
def set_config(key, value)
store.transaction do
store[CONF_NS] ||= {}

View File

@ -1,6 +1,26 @@
require "spec_helper"
RSpec.describe Pigeon::Message do
it "signs a message" do
message = Pigeon::Message.create(kind: "unit_test")
hash = Pigeon::Storage.current.set_blob(File.read("./logo.png"))
expectations = {
author: Pigeon::KeyPair.current.public_key,
kind: "unit_test",
body: {
"a" => "bar".to_json,
"b" => hash,
},
depth: 0,
prev: Pigeon::Message::EMPTY_MESSAGE,
}
message.append("a", "bar")
message.append("b", hash)
message.sign
raise "need better assertions!"
end
it "renders a message"
it "creates a new message" do
message = Pigeon::Message.create(kind: "unit_test")
hash = Pigeon::Storage.current.set_blob(File.read("./logo.png"))
@ -8,14 +28,14 @@ RSpec.describe Pigeon::Message do
author: Pigeon::KeyPair.current.public_key,
kind: "unit_test",
body: {
"foo" => "bar".to_json,
"baz" => hash,
"a" => "bar".to_json,
"b" => hash,
},
depth: 0,
prev: Pigeon::Message::EMPTY_MESSAGE,
}
message.append("foo", "bar")
message.append("baz", hash)
message.append("a", "bar")
message.append("b", hash)
expect(message.author).to eq(Pigeon::KeyPair.current.public_key)
expect(message.kind).to eq("unit_test")
expect(message.body).to eq(expectations.fetch(:body))