Project

General

Profile

Download (4.57 KB) Statistics
| Branch: | Tag: | Revision:
require 'spec_helper'

require 'cyborghood/mail'
require 'tmpdir'
require 'mocha/setup'


CyborgHood::Config.load("postman")


describe CyborgHood::Mail do
before do
@config = CyborgHood::Config.instance
end

KEY_NAME = "My Test"
KEY_PASSPHRASE = "stuff"
KEY_EMAIL = "taiste@mydom.tld"

def work_with_key
@gpgdir = Dir.mktmpdir
ENV['GNUPGHOME'] = @gpgdir
ENV['GPG_AGENT_INFO'] = nil

GPGME::Ctx.new do |ctx|
ctx.genkey(<<-EOF, nil, nil)
<GnupgKeyParms format="internal">
Key-Type: DSA
Key-Length: 1024
Subkey-Type: ELG-E
Subkey-Length: 1024
Name-Real: #{KEY_NAME}
Name-Comment: CyborgHood test key
Name-Email: #{KEY_EMAIL}
Expire-Date: 0
</GnupgKeyParms>
EOF
# don't set a passphrase, unusable since GPGME linked with GnuPG2
# Passphrase: #{KEY_PASSPHRASE}
key = ctx.keys.first
@key_id = key.subkeys.first.fingerprint

yield(ctx)
end

FileUtils.remove_entry_secure @gpgdir
end

describe "basics" do
before do
@mail_empty = CyborgHood::Mail.new
end

it "should have GPG support" do
@mail_empty.must_respond_to :is_pgp_signed?
@mail_empty.must_respond_to :is_pgp_encrypted?
@mail_empty.must_respond_to :verify_pgp_signature
@mail_empty.must_respond_to :pgp_signed_part
@mail_empty.must_respond_to :pgp_decrypt
@mail_empty.must_respond_to :create_signed
@mail_empty.must_respond_to :create_encrypted
end

it "should be able to create an empty clear mail" do
@mail_empty.body.decoded.must_be_empty
@mail_empty.is_pgp_signed?.must_equal false
@mail_empty.is_pgp_encrypted?.must_equal false
end

it "should set custom headers" do
@mail_empty['Organization'].to_s.must_equal @config.mail.organization
end
end

describe "parsing emails" do
it "should parse a clear email" do
CyborgHood::Mail.read(fixture('emails', 'mail_clear.eml'))
end

it "should parse a signed email" do
CyborgHood::Mail.read(fixture('emails', 'mail_signed.eml'))
end
end

describe "creating replies" do
before do
@test_from = "z@z.net"

@mail = CyborgHood::Mail.new
@mail.subject = "test"
@mail.to = KEY_EMAIL
@mail.from = @test_from
@mail.body = "hello world!"
@mail.add_message_id
end

it "should create a reply" do
@mail.user = mock()

mail_reply = @mail.reply

# must_be_kind_of works badly with Delegator
mail_reply.class.must_equal CyborgHood::Mail
mail_reply.to.must_equal @mail.from
mail_reply.user.must_equal @mail.user
end
end

describe "unsecure emails handling" do
it "should reject an unsecure mail" do
mail_clear = CyborgHood::Mail.read(fixture('emails', 'mail_clear.eml'))

r = mail_clear.process
r.must_be_kind_of CyborgHood::MailReport
r.ok?.must_equal false
end
end

describe "secure emails handling" do
before do
@mail = CyborgHood::Mail.new
@mail.subject = "test"
@mail.to = KEY_EMAIL
@mail.body = "hello world!"
@mail.add_message_id
end

def create_fake_user
fake_user_uid = 'test'

fake_user = mock()
fake_user.expects(:uid).returns(fake_user_uid).at_least(2)
fake_user.expects(:cn).returns('Test User').at_least_once
CyborgHood::Person.stubs(:find_by_fingerprint).with(@key_id).returns(fake_user)

fake_user_uid
end

it "should process a signed mail and identify sender" do
work_with_key do |ctx|
mail_signed = @mail.create_signed(@key_id) { KEY_PASSPHRASE }
mail_recv = CyborgHood::Mail.new(mail_signed.encoded)

mail_recv.is_pgp_signed?.must_equal true

fake_user_uid = create_fake_user()
r = mail_recv.process
r.must_be_kind_of CyborgHood::MailReport
r.ok?.must_equal true

user = r.user
user.wont_be_nil
user.uid.must_equal fake_user_uid
end
end

it "should create a properly signed mail, process it, and identify sender" do
work_with_key do |ctx|
@config.mail.key_id = @key_id
@config.mail.key_passphrase = KEY_PASSPHRASE
mail_signed = @mail.sign
mail_recv = CyborgHood::Mail.new(mail_signed.encoded)

mail_recv.is_pgp_signed?.must_equal true

fake_user_uid = create_fake_user()
r = mail_recv.process
r.must_be_kind_of CyborgHood::MailReport
r.ok?.must_equal true

user = r.user
user.wont_be_nil
user.uid.must_equal fake_user_uid
end
end
end
end
    (1-1/1)