This repository was archived by the owner on Nov 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmessages_controller_spec.rb
168 lines (149 loc) · 7.02 KB
/
messages_controller_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
require "rails_helper"
email = "hello@encrypt.to"
expired_email = "expired@encrypt.to"
unknown_email = "unknown@encrypt.to"
invalid_email = "test@encrypt.to.to"
short_keyid = "0x11489a1f"
long_keyid = "0x0caf1e5b11489a1f"
invalid_keyid = "0x0x11489a1f"
vindex_response = "info:1:2\npub:11489A1F:1:2048:1387447945:1587447945:\nuid:Encrypt.to <hello@encrypt.to>:1387447945::\n\r\n"
vindex_response_expired = "info:1:2\npub:11489A1F:1:2048:1387447945:1287447945:\nuid:Encrypt.to <expired@encrypt.to>:1387447945::\n\r\n"
vindex_response_unknown = "info:1:2\npub:11489A1F:1:2048:1387447945::\nuid:Encrypt.to <unknown@encrypt.to>:1387447945::\n\r\n"
vindex_response_invalid = ""
message = "-----BEGIN PGP MESSAGE-----\nVersion: OpenPGP.js v.1.20130306\nComment: http://openpgpjs.org\n\nwcBMA3VR7lR02L1RAQf/T/DHX8b1Ka65EpXZcffKjgzYch11Kvm0SJcXne0G\n2M/k3vAsKnru+zsbOnV+9IpXIywJIyDWOFasrqZggmHlMVOSr5CjKX27RspY\nfRPJ/9AU+Oada0iqocMIexY1QkoeGO16je0QWd7sbq+ejZZbJwfSvG/orW87\nHhX/r0pfUEpcwSNQcc4588NQ6qRvi9QwXt+Ykktozqi+JGurWOotLwe4/SQk\nJ2PePxYX6hBP1mUW7WVIHL3imM44Fe4x8yhFCVWpZDeKY1aA4B5Sg4STuuCJ\nnUgnpoeC4lDX+PyEoFq+QUi1sTHWdrZq6u8LUYX/Ode6tW/olVxYOoabWZ3y\nUtI4AZITMAgOOeDueWxbR214x3wqMQc7W1IuZWpzL4ogE+zjWwHU1j6EgD31\npEnyQbmBDMgGlxPqcis=\n=W15x\n-----END PGP MESSAGE-----"
keyserver_url = "http://pgpkey.org/pks/lookup"
describe MessagesController, type: :controller do
let(:user) { create :user }
before {
stub_request(:get, keyserver_url + "?exact=on&op=vindex&options=mr&search=#{email}").
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => vindex_response, :headers => {})
stub_request(:get, keyserver_url + "?exact=on&op=vindex&options=mr&search=#{expired_email}").
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => vindex_response_expired, :headers => {})
stub_request(:get, keyserver_url + "?exact=on&op=vindex&options=mr&search=#{unknown_email}").
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => vindex_response_unknown, :headers => {})
stub_request(:get, keyserver_url + "?exact=on&op=vindex&options=mr&search=#{invalid_email}").
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => vindex_response_invalid, :headers => {})
stub_request(:get, keyserver_url + "?op=get&options=mr&search=#{short_keyid}").
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => user.public_key, :headers => {})
stub_request(:get, keyserver_url + "?fingerprint=on&op=vindex&options=mr&search=#{short_keyid}").
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => vindex_response, :headers => {})
stub_request(:get, keyserver_url + "?fingerprint=on&op=vindex&options=mr&search=#{long_keyid}").
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => vindex_response, :headers => {})
stub_request(:get, keyserver_url + "?op=get&options=mr&search=#{long_keyid}").
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => user.public_key, :headers => {})
stub_request(:get, keyserver_url + "?op=get&options=mr&search=#{invalid_keyid}").
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => vindex_response_invalid, :headers => {})
stub_request(:get, keyserver_url + "?fingerprint=on&op=vindex&options=mr&search=#{invalid_keyid}").
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => vindex_response_invalid, :headers => {})
}
describe "GET new" do
it "has a 302 status code if params empty" do
get :new
expect(response.status).to eq(302)
expect(flash[:notice]).to eq(I18n.t('messages.new.invalid_link'))
end
end
describe "GET new" do
it "has a 302 status code if params invalid email" do
get :new, uid: invalid_email
expect(response.status).to eq(302)
expect(flash[:notice]).to eq(I18n.t('messages.new.no_public_key'))
end
end
describe "GET new" do
it "has a 302 status code if params invalid key-id" do
get :new, uid: invalid_keyid
expect(response.status).to eq(302)
expect(flash[:notice]).to eq(I18n.t('messages.new.no_mail'))
end
end
describe "GET new" do
it "has a 200 status code if params uid is local user" do
get :new, uid: user.username
expect(assigns(:pubkey)).to eq(user.public_key)
expect(response.status).to eq(200)
end
end
describe "GET new" do
it "has a 200 status code if params uid is email" do
get :new, uid: email
expect(response.status).to eq(200)
expect(assigns(:pubkey)).to eq(user.public_key)
end
end
describe "GET new" do
it "has a 302 status code if params uid is an expired email" do
get :new, uid: expired_email
expect(response.status).to eq(302)
expect(flash[:notice]).to eq(I18n.t('messages.new.no_public_key'))
end
end
describe "GET new" do
it "has a 200 status code if params uid is an unknown expired date email" do
get :new, uid: unknown_email
expect(response.status).to eq(200)
expect(assigns(:pubkey)).to eq(user.public_key)
end
end
describe "GET new" do
it "has a 200 status code if params uid is email upcase" do
get :new, uid: email.upcase
expect(response.status).to eq(200)
expect(assigns(:pubkey)).to eq(user.public_key)
end
end
describe "GET new" do
it "has a 200 status code if params uid is short keyid" do
get :new, uid: short_keyid
expect(response.status).to eq(200)
expect(assigns(:pubkey)).to eq(user.public_key)
end
end
describe "GET new" do
it "has a 200 status code if params uid is short keyid upcase" do
get :new, uid: short_keyid.upcase
expect(response.status).to eq(200)
expect(assigns(:pubkey)).to eq(user.public_key)
end
end
describe "GET new" do
it "has a 200 status code if params uid is long keyid" do
get :new, uid: long_keyid
expect(response.status).to eq(200)
expect(assigns(:pubkey)).to eq(user.public_key)
end
end
describe "GET new" do
it "has a 200 status code if params uid is long keyid upcase" do
get :new, uid: long_keyid.upcase
expect(response.status).to eq(200)
expect(assigns(:pubkey)).to eq(user.public_key)
end
end
describe "POST message" do
it "has a 302 status code if params are valid" do
ActionMailer::Base.deliveries.clear
from = email
post :create, :message => {to: email, from: from, body: message}
expect(response.status).to eq(302)
end
end
describe "POST message" do
it "has a 302 status code if params are invalid" do
message = ""
from = email
post :create, :message => {to: user.email, from: from, body: message}
expect(response.status).to eq(302)
end
end
end