Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
5a00664679b8
Commit
c1281982
authored
Feb 28, 2017
by
Alexis Reigel
Browse files
notification email on add new gpg key
parent
c169b79130f8
Changes
9
Hide whitespace changes
Inline
Side-by-side
app/mailers/emails/profile.rb
View file @
5a006646
...
...
@@ -22,5 +22,15 @@ def new_ssh_key_email(key_id)
@target_url
=
user_url
(
@user
)
mail
(
to:
@user
.
notification_email
,
subject:
subject
(
"SSH key was added to your account"
))
end
def
new_gpg_key_email
(
gpg_key_id
)
@gpg_key
=
GpgKey
.
find_by_id
(
gpg_key_id
)
return
unless
@gpg_key
@current_user
=
@user
=
@gpg_key
.
user
@target_url
=
user_url
(
@user
)
mail
(
to:
@user
.
notification_email
,
subject:
subject
(
"GPG key was added to your account"
))
end
end
end
app/models/gpg_key.rb
View file @
5a006646
class
GpgKey
<
ActiveRecord
::
Base
include
AfterCommitQueue
KEY_PREFIX
=
'-----BEGIN PGP PUBLIC KEY BLOCK-----'
.
freeze
belongs_to
:user
...
...
@@ -20,6 +22,7 @@ class GpgKey < ActiveRecord::Base
before_validation
:extract_fingerprint
after_create
:add_to_keychain
after_create
:notify_user
after_destroy
:remove_from_keychain
def
key
=
(
value
)
...
...
@@ -62,4 +65,8 @@ def add_to_keychain
def
remove_from_keychain
Gitlab
::
Gpg
::
CurrentKeyChain
.
remove
(
fingerprint
)
end
def
notify_user
run_after_commit
{
NotificationService
.
new
.
new_gpg_key
(
self
)
}
end
end
app/services/notification_service.rb
View file @
5a006646
...
...
@@ -17,6 +17,16 @@ def new_key(key)
end
end
# Always notify the user about gpg key added
#
# This is a security email so it will be sent even if the user user disabled
# notifications
def
new_gpg_key
(
gpg_key
)
if
gpg_key
.
user
mailer
.
new_gpg_key_email
(
gpg_key
.
id
).
deliver_later
end
end
# Always notify user about email added to profile
def
new_email
(
email
)
if
email
.
user
...
...
app/views/notify/new_gpg_key_email.html.haml
0 → 100644
View file @
5a006646
%p
Hi
#{
@user
.
name
}
!
%p
A new GPG key was added to your account:
%p
Fingerprint:
%code
=
@gpg_key
.
fingerprint
%p
If this key was added in error, you can remove it under
=
link_to
"GPG Keys"
,
profile_gpg_keys_url
app/views/notify/new_gpg_key_email.text.erb
0 → 100644
View file @
5a006646
Hi
<%=
@user
.
name
%>
!
A new GPG key was added to your account:
Fingerprint:
<%=
@gpg_key
.
fingerprint
%>
If this key was added in error, you can remove it at
<%=
profile_gpg_keys_url
%>
spec/factories/gpg_keys.rb
View file @
5a006646
...
...
@@ -3,5 +3,6 @@
FactoryGirl
.
define
do
factory
:gpg_key
do
key
GpgHelpers
::
User1
.
public_key
user
end
end
spec/mailers/emails/profile_spec.rb
View file @
5a006646
...
...
@@ -91,6 +91,36 @@
end
end
describe
'user added gpg key'
do
let
(
:gpg_key
)
{
create
(
:gpg_key
)
}
subject
{
Notify
.
new_gpg_key_email
(
gpg_key
.
id
)
}
it_behaves_like
'an email sent from GitLab'
it_behaves_like
'it should not have Gmail Actions links'
it_behaves_like
'a user cannot unsubscribe through footer link'
it
'is sent to the new user'
do
is_expected
.
to
deliver_to
gpg_key
.
user
.
email
end
it
'has the correct subject'
do
is_expected
.
to
have_subject
/^GPG key was added to your account$/i
end
it
'contains the new gpg key title'
do
is_expected
.
to
have_body_text
/
#{
gpg_key
.
fingerprint
}
/
end
it
'includes a link to gpg keys page'
do
is_expected
.
to
have_body_text
/
#{
profile_gpg_keys_path
}
/
end
context
'with GPG key that does not exist'
do
it
{
expect
{
Notify
.
new_gpg_key_email
(
'foo'
)
}.
not_to
raise_error
}
end
end
describe
'user added email'
do
let
(
:email
)
{
create
(
:email
)
}
...
...
spec/models/gpg_key_spec.rb
View file @
5a006646
...
...
@@ -103,4 +103,18 @@
end
end
end
describe
'notification'
do
include
EmailHelpers
let
(
:user
)
{
create
(
:user
)
}
it
'sends a notification'
do
perform_enqueued_jobs
do
create
(
:gpg_key
,
user:
user
)
end
should_email
(
user
)
end
end
end
spec/services/notification_service_spec.rb
View file @
5a006646
...
...
@@ -93,6 +93,18 @@ def send_notifications(*new_mentions)
end
end
describe
'GpgKeys'
do
describe
'#new_gpg_key'
do
let!
(
:key
)
{
create
(
:gpg_key
)
}
it
{
expect
(
notification
.
new_gpg_key
(
key
)).
to
be_truthy
}
it
'sends email to key owner'
do
expect
{
notification
.
new_gpg_key
(
key
)
}.
to
change
{
ActionMailer
::
Base
.
deliveries
.
size
}.
by
(
1
)
end
end
end
describe
'Email'
do
describe
'#new_email'
do
let!
(
:email
)
{
create
(
:email
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment