Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kazuhiko Shiozaki
gitlab-ce
Commits
8ec5fb13
Commit
8ec5fb13
authored
Aug 20, 2015
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Test Gitlab::Email::Receiver.
parent
e44936f3
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
144 additions
and
9 deletions
+144
-9
lib/gitlab/email/receiver.rb
lib/gitlab/email/receiver.rb
+9
-9
spec/lib/gitlab/email/receiver_spec.rb
spec/lib/gitlab/email/receiver_spec.rb
+135
-0
No files found.
lib/gitlab/email/receiver.rb
View file @
8ec5fb13
...
@@ -16,17 +16,11 @@ module Gitlab
...
@@ -16,17 +16,11 @@ module Gitlab
@raw
=
raw
@raw
=
raw
end
end
def
message
@message
||=
Mail
::
Message
.
new
(
@raw
)
rescue
Encoding
::
UndefinedConversionError
,
Encoding
::
InvalidByteSequenceError
=>
e
raise
EmailUnparsableError
,
e
end
def
execute
def
execute
raise
SentNotificationNotFoundError
unless
sent_notification
raise
EmptyEmailError
if
@raw
.
blank?
raise
EmptyEmailError
if
@raw
.
blank?
raise
SentNotificationNotFoundError
unless
sent_notification
raise
AutoGeneratedEmailError
if
message
.
header
.
to_s
=~
/auto-(generated|replied)/
raise
AutoGeneratedEmailError
if
message
.
header
.
to_s
=~
/auto-(generated|replied)/
author
=
sent_notification
.
recipient
author
=
sent_notification
.
recipient
...
@@ -35,7 +29,7 @@ module Gitlab
...
@@ -35,7 +29,7 @@ module Gitlab
project
=
sent_notification
.
project
project
=
sent_notification
.
project
raise
UserNotAuthorizedError
unless
author
.
can?
(
:create_note
,
project
)
raise
UserNotAuthorizedError
unless
project
&&
author
.
can?
(
:create_note
,
project
)
raise
NoteableNotFoundError
unless
sent_notification
.
noteable
raise
NoteableNotFoundError
unless
sent_notification
.
noteable
...
@@ -59,6 +53,12 @@ module Gitlab
...
@@ -59,6 +53,12 @@ module Gitlab
private
private
def
message
@message
||=
Mail
::
Message
.
new
(
@raw
)
rescue
Encoding
::
UndefinedConversionError
,
Encoding
::
InvalidByteSequenceError
=>
e
raise
EmailUnparsableError
,
e
end
def
reply_key
def
reply_key
reply_key
=
nil
reply_key
=
nil
message
.
to
.
each
do
|
address
|
message
.
to
.
each
do
|
address
|
...
...
spec/lib/gitlab/email/receiver_spec.rb
0 → 100644
View file @
8ec5fb13
require
"spec_helper"
describe
Gitlab
::
Email
::
Receiver
do
def
fixture_file
(
filename
)
return
''
if
filename
.
blank?
file_path
=
File
.
expand_path
(
Rails
.
root
+
'spec/fixtures/'
+
filename
)
File
.
read
(
file_path
)
end
before
do
allow
(
Gitlab
.
config
.
reply_by_email
).
to
receive
(
:enabled
).
and_return
(
true
)
allow
(
Gitlab
.
config
.
reply_by_email
).
to
receive
(
:address
).
and_return
(
"reply+%{reply_key}@appmail.adventuretime.ooo"
)
end
let
(
:reply_key
)
{
"59d8df8370b7e95c5a49fbf86aeb2c93"
}
let
(
:email_raw
)
{
fixture_file
(
'emails/valid_reply.eml'
)
}
let
(
:project
)
{
create
(
:project
,
:public
)
}
let
(
:noteable
)
{
create
(
:issue
,
project:
project
)
}
let
(
:user
)
{
create
(
:user
)
}
let!
(
:sent_notification
)
{
SentNotification
.
record
(
noteable
,
user
.
id
,
reply_key
)
}
let
(
:receiver
)
{
described_class
.
new
(
email_raw
)
}
context
"when the recipient address doesn't include a reply key"
do
let
(
:email_raw
)
{
fixture_file
(
'emails/valid_reply.eml'
).
gsub
(
reply_key
,
""
)
}
it
"raises a SentNotificationNotFoundError"
do
expect
{
receiver
.
execute
}.
to
raise_error
(
Gitlab
::
Email
::
Receiver
::
SentNotificationNotFoundError
)
end
end
context
"when no sent notificiation for the reply key could be found"
do
let
(
:email_raw
)
{
fixture_file
(
'emails/valid_reply.eml'
).
gsub
(
reply_key
,
"nope"
)
}
it
"raises a SentNotificationNotFoundError"
do
expect
{
receiver
.
execute
}.
to
raise_error
(
Gitlab
::
Email
::
Receiver
::
SentNotificationNotFoundError
)
end
end
context
"when the email is blank"
do
let
(
:email_raw
)
{
""
}
it
"raises an EmptyEmailError"
do
expect
{
receiver
.
execute
}.
to
raise_error
(
Gitlab
::
Email
::
Receiver
::
EmptyEmailError
)
end
end
context
"when the email was auto generated"
do
let!
(
:reply_key
)
{
'636ca428858779856c226bb145ef4fad'
}
let!
(
:email_raw
)
{
fixture_file
(
"emails/auto_reply.eml"
)
}
it
"raises an AutoGeneratedEmailError"
do
expect
{
receiver
.
execute
}.
to
raise_error
(
Gitlab
::
Email
::
Receiver
::
AutoGeneratedEmailError
)
end
end
context
"when the user could not be found"
do
before
do
user
.
destroy
end
it
"raises a UserNotFoundError"
do
expect
{
receiver
.
execute
}.
to
raise_error
(
Gitlab
::
Email
::
Receiver
::
UserNotFoundError
)
end
end
context
"when the user is not authorized to create a note"
do
before
do
project
.
update_attribute
(
:visibility_level
,
Project
::
PRIVATE
)
end
it
"raises a UserNotAuthorizedError"
do
expect
{
receiver
.
execute
}.
to
raise_error
(
Gitlab
::
Email
::
Receiver
::
UserNotAuthorizedError
)
end
end
context
"when the noteable could not be found"
do
before
do
noteable
.
destroy
end
it
"raises a NoteableNotFoundError"
do
expect
{
receiver
.
execute
}.
to
raise_error
(
Gitlab
::
Email
::
Receiver
::
NoteableNotFoundError
)
end
end
context
"when the reply is blank"
do
let!
(
:email_raw
)
{
fixture_file
(
"emails/no_content_reply.eml"
)
}
it
"raises an EmptyEmailError"
do
expect
{
receiver
.
execute
}.
to
raise_error
(
Gitlab
::
Email
::
Receiver
::
EmptyEmailError
)
end
end
context
"when the note could not be saved"
do
before
do
allow_any_instance_of
(
Note
).
to
receive
(
:persisted?
).
and_return
(
false
)
end
it
"raises an InvalidNoteError"
do
expect
{
receiver
.
execute
}.
to
raise_error
(
Gitlab
::
Email
::
Receiver
::
InvalidNoteError
)
end
end
context
"when everything is fine"
do
before
do
allow_any_instance_of
(
Gitlab
::
Email
::
AttachmentUploader
).
to
receive
(
:execute
).
and_return
(
[
{
url:
"uploads/image.png"
,
is_image:
true
,
alt:
"image"
}
]
)
end
it
"creates a comment"
do
expect
{
receiver
.
execute
}.
to
change
{
noteable
.
notes
.
count
}.
by
(
1
)
note
=
noteable
.
notes
.
last
expect
(
note
.
author
).
to
eq
(
sent_notification
.
recipient
)
expect
(
note
.
note
).
to
include
(
"I could not disagree more."
)
end
it
"adds all attachments"
do
receiver
.
execute
note
=
noteable
.
notes
.
last
expect
(
note
.
note
).
to
include
(
"![image](uploads/image.png)"
)
end
end
end
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