Skip to content

Commit

Permalink
changes to the api calls and specs for integrations_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperionel authored and Chocksy committed Jul 13, 2016
1 parent ab053ee commit 1876099
Show file tree
Hide file tree
Showing 19 changed files with 231 additions and 102 deletions.
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,3 @@ DEPENDENCIES
warden_omniauth
webmock
wicked

BUNDLED WITH
1.11.2
8 changes: 4 additions & 4 deletions app/controllers/auth_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AuthController < ApplicationController

def create
unless Integrations.supports?(integration_params[:provider])
redirect_to installations_path(main_tab: 'integrations'), notice: 'Invalid Integration'
redirect_to settings_path(main_tab: 'integrations'), notice: 'Invalid Integration'
return
end

Expand All @@ -14,7 +14,7 @@ def create
session['integration'] = integration_params
redirect_to "/auth/#{@integration.provider}"
else
redirect_to installations_path(main_tab: 'integrations'), notice: 'Integration parameters are invalid!'
redirect_to settings_path(main_tab: 'integrations'), notice: 'Integration parameters are invalid!'
end
end

Expand All @@ -33,10 +33,10 @@ def success
@integration = current_website.integrations.build(integration_session)
@integration.assign_configuration(auth_hash)
@integration.save!
redirect_to installations_path(main_tab: 'integrations', integration_tab: provider ), notice: 'Integration created'
redirect_to settings_path(main_tab: 'integrations', integration_tab: provider ), notice: 'Integration created'
end
rescue
redirect_to installations_path(main_tab: 'integrations'), flash: { error: 'Error creating integration' }
redirect_to settings_path(main_tab: 'integrations'), flash: { error: 'Error creating integration' }
ensure
session['integration'] = nil
end
Expand Down
16 changes: 13 additions & 3 deletions app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,19 @@ def update

def notify_subscribers
unless params[:message].blank?
@message = Message.create( content: params[:message], issue_id: @error.issues.last.id )
@error.subscribers.each do |subscriber|
GroupedIssueMailer.notify_subscriber(@error, subscriber, current_user, @message).deliver_later
if params[:intercom]
begin
current_website.intercom_integration.driver.send_message(params[:users], params[:message])
redirect_to error_path(@error), flash: { success: 'Message successfully sent!' }
rescue => e
redirect_to error_path(@error), flash: { error: 'Operation failed!' }
end
else
@message = Message.create( content: params[:message], issue_id: @error.issues.last.id )
@error.subscribers.each do |subscriber|
GroupedIssueMailer.notify_subscriber(@error, subscriber, current_user, @message).deliver_later
end
redirect_to error_path(@error), flash: { success: 'Message successfully sent!' }
end
end
end
Expand Down
19 changes: 5 additions & 14 deletions app/controllers/integrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,23 @@ class IntegrationsController < ApplicationController

def update
if @integration.update_attributes(integration_params)
redirect_to installations_path(main_tab: 'integrations', integration_tab: @integration.provider), notice: 'Integration updated!'
redirect_to settings_path(main_tab: 'integrations', integration_tab: @integration.provider), notice: 'Integration updated!'
end
end

def destroy
@integration.destroy
redirect_to installations_path(main_tab: 'integrations', integration_tab: @integration.provider), notice: 'Integration deleted!'
if @integration.destroy
redirect_to settings_path(main_tab: 'integrations', integration_tab: @integration.provider), notice: 'Integration deleted!'
end
end

def create_task
begin
task = @integration.driver.create_task(params[:title])
redirect_to error_path(params[:error_id], task: task)
rescue
redirect_to error_path(params[:error_id], task: task), flash: { error: "Operation failed!" }
end
redirect_to error_path(params[:error_id], task: task)
end

def notify_intercom_subscribers
begin
@integration.driver.send_message(params[:users], params[:message])
rescue => e
response = JSON.parse(e.response)
redirect_to error_path(params[:error_id]), flash: { error: response['errors']['message'] }
end
redirect_to error_path(params[:error_id]), flash: { success: 'Message successfully sent!' }
end

def integration_params
Expand Down
2 changes: 1 addition & 1 deletion app/views/errors/_overview.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
This message will be sent to
= intercom_users.size.to_s + (intercom_users.size > 1 ? ' users' : ' user')
.notify
= form_for @error, url: notify_intercom_subscribers_integration_path(current_website.intercom_integration, users: intercom_users, error_id: @error.id), remote: true, html: { method: :post , class: ' broadcast-textarea'}, :data => {:confirm => 'Are you sure?'} do |f|
= form_for @error, url: notify_subscribers_error_path(current_website.intercom_integration, users: intercom_users, intercom: true), remote: true, html: { method: :post , class: ' broadcast-textarea'}, :data => {:confirm => 'Are you sure?'} do |f|
= text_area_tag 'message', '', placeholder: 'Write a message ...'
%button.btn.btn-primary{:type => 'submit'} Send
- else
Expand Down
1 change: 0 additions & 1 deletion app/views/errors/notify_subscribers.js.haml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
= select_tag "integration[application]", grouped_options_for_select(@integration.get_applications), { class: "pull-left selectpicker" }
= submit_tag "Submit", { class: "pull-right col-sm-4 btn btn-default btn-primary"}
- else
= 'Your app is ' + @integration.selected_application
= 'Your repository is ' + @integration.selected_application
2 changes: 1 addition & 1 deletion app/views/settings/header_tabs/_integrations.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
.panel-heading
%span
=provider.to_s.capitalize + " Integration"
- if !@integration.nil?
- unless @integration.nil?
= button_to integration_path(@integration), method: :delete , class: 'btn btn-default remove-button }' do
%i.fa.fa-times
.panel-body
Expand Down
1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
resources :integrations, only: [:update, :destroy] do
member do
post :create_task
post :notify_intercom_subscribers
end
end
resources :grouped_issues, only: [:index, :show]
Expand Down
8 changes: 8 additions & 0 deletions lib/integrations/base_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,13 @@ def build_configuration(auth_hash)
end
config
end

def get_request(url)
RestClient.get url, header rescue 'Operation failed!'
end

def post_request(url, data)
RestClient.post url, data.to_json, header rescue 'Operation failed!'
end
end
end
16 changes: 8 additions & 8 deletions lib/integrations/drivers/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,18 @@ def api_url
end

def create_task(title)
configuration = self.configuration
url = api_url + 'repos/' + configuration["username"].to_s + '/' + configuration["selected_application"].to_s + '/issues'
payload = { title: title }.to_json
headers = { "Authorization": "token " + configuration["token"].to_s, "Content-Type": "application/json" }
response = RestClient.post url, payload, headers rescue nil
url = api_url + 'repos/' + self.configuration["username"].to_s + '/' + self.configuration["selected_application"].to_s + '/issues'
data = { title: title }
response = post_request(url, data)
if response
response = JSON.parse(response)
response
end
end

def applications
configuration = self.configuration
url = api_url + 'user/repos?affiliation=owner,organization_member'
headers = { "Authorization": "token " + configuration["token"].to_s }
response = RestClient.get url, headers rescue nil
response = get_request(url)
if response
repos = { "Owner": [] }
JSON.parse(response).each do |repo|
Expand All @@ -47,6 +43,10 @@ def applications
end
end

def header
{ "Authorization": "token #{self.configuration["token"]}", "Content-Type": "application/json", "Accept": "application/json" }
end

def selected_application
self.configuration['selected_application']
end
Expand Down
19 changes: 10 additions & 9 deletions lib/integrations/drivers/intercom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ def type
end

def list_subscribers
config = self.configuration
url = api_url + 'users'
users = []
resource = RestClient::Resource.new( api_url + 'users', config['token'], nil)
response = resource.get( :content_type => :json, :accept => :json)
response = self.get_request(url)
response = JSON.parse(response)
response['users'].each do |user|
users.push( { email: user['email'], avatar: user['avatar']['image_url'] } )
Expand All @@ -24,31 +23,33 @@ def list_subscribers
end

def send_message(users = [], message)
config = self.configuration
url = api_url + 'messages'
auth = 'Basic ' + Base64.strict_encode64( "#{config['token']}:" ).chomp
headers = { "Authorization": auth , "Content-Type": "application/json", "Accept": "application/json" }
responses = []
users.each do |user|
payload = {
data = {
"message_type" => "inapp",
"body" => message,
"template" => "plain",
"from" => {
"type" => "admin",
"id" => config["uid"]
"id" => self.configuration["uid"]
},
"to" => {
"type" => "user",
"email" => user['email']
}
}
resource = RestClient.post url, payload.to_json, headers
resource = self.post_request(url, data)
responses.push(JSON.parse(resource.body))
end
responses
end

def header
auth = 'Basic ' + Base64.strict_encode64( "#{self.configuration['token']}:" ).chomp
{ "Authorization": auth , "Content-Type": "application/json", "Accept": "application/json" }
end

def api_url
'https://api.intercom.io/'
end
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/auth_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
default_params[:integration][:name] = "nodriver"
default_params[:integration][:provider] = "nodriver"
post_with user, :create, default_params
expect(response).to redirect_to(installations_path(main_tab: 'integrations'))
expect(response).to redirect_to(settings_path(main_tab: 'integrations'))
expect(flash[:notice]).to eq('Invalid Integration')
end
end
Expand All @@ -25,7 +25,7 @@
it "redirects to integrations page if the integration could not be built" do
params[:integration].delete(:name)
post_with user, :create, params
expect(response).to redirect_to(installations_path(main_tab: 'integrations'))
expect(response).to redirect_to(settings_path(main_tab: 'integrations'))
expect(flash[:notice]).to eq('Integration parameters are invalid!')
end
end
Expand Down Expand Up @@ -118,15 +118,15 @@
it "redirects to the installations path" do
session[:integration] = { "name" => "Github", "provider" => "github" }
get :success, provider: 'github'
expect(response).to redirect_to(installations_path(main_tab: 'integrations', integration_tab: omniauth_hash['provider'] ))
expect(response).to redirect_to(settings_path(main_tab: 'integrations', integration_tab: omniauth_hash['provider'] ))
end

it "throws error if the integration cannot be built" do
request.env['omniauth.auth'].delete('extra')
session[:integration] = { "name" => "Github", "provider" => "github" }
get :success, provider: 'github'
expect(flash[:error]).to eq("Error creating integration")
expect(response).to redirect_to(installations_path(main_tab: 'integrations'))
expect(response).to redirect_to(settings_path(main_tab: 'integrations'))
end
end
end
Expand Down
98 changes: 73 additions & 25 deletions spec/controllers/errors_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,77 @@
context 'if logged in' do
let(:params) { { message: 'message that is going to create record', id: group.id, format: :js } }

it 'should email subscribers' do
params[:format] = 'js'
mailer = double('GroupedIssueMailer')
expect(mailer).to receive(:deliver_later)
expect(GroupedIssueMailer).to receive(:notify_subscriber).with(group, subscriber, user, an_instance_of(Message)).and_return(mailer).once
context 'email' do
it 'should email subscribers' do
mailer = double('GroupedIssueMailer')
expect(mailer).to receive(:deliver_later)
expect(GroupedIssueMailer).to receive(:notify_subscriber).with(group, subscriber, user, an_instance_of(Message)).and_return(mailer).once

post_with user, :notify_subscribers, params
end
post_with user, :notify_subscribers, params
end

it 'should email 2 subscribers' do
subscriber2 = create :subscriber, website: website
issue2 = create :issue, subscriber: subscriber2, group: group
mailer = double('GroupedIssueMailer')
expect(mailer).to receive(:deliver_later).twice
expect(GroupedIssueMailer).to receive(:notify_subscriber).with(group, an_instance_of(Subscriber), an_instance_of(User), an_instance_of(Message)).and_return(mailer).twice
post_with user, :notify_subscribers, params
end
it 'should email 2 subscribers' do
subscriber2 = create :subscriber, website: website
issue2 = create :issue, subscriber: subscriber2, group: group
mailer = double('GroupedIssueMailer')
expect(mailer).to receive(:deliver_later).twice
expect(GroupedIssueMailer).to receive(:notify_subscriber).with(group, an_instance_of(Subscriber), an_instance_of(User), an_instance_of(Message)).and_return(mailer).twice
post_with user, :notify_subscribers, params
end

it 'assigns message' do
post_with user, :notify_subscribers, params
expect(subject.instance_variable_get(:@message)).to be_an_instance_of(Message)
end

it 'assigns message' do
post_with user, :notify_subscribers, params
expect(subject.instance_variable_get(:@message)).to be_an_instance_of(Message)
it 'redirects to the error path with success message' do
post_with user, :notify_subscribers, params
expect(response).to redirect_to(error_path(id: group.id))
expect(flash[:success]).to eq('Message successfully sent!')
end
end
end

describe 'GET #index' do
let(:params) { { current_issue: issue_error.id } }
context 'intercom' do
let!(:integration) { create :integration, website: website, provider: :intercom }
let(:driver) { Integrations.get_driver(integration.provider.to_sym) }
let(:integration_driver) { driver.new(Integrations::Integration.new(integration, driver)) }
before(:each) do session[:epiclogger_website_id] = website.id end
let(:intercom_params) { params.merge( intercom: true, users: [ { 'email' => 'zoro' } ] ) }

context 'success' do
before(:each) do
stub_request(:post, integration_driver.api_url + 'messages')
.with(:body => "{\"message_type\":\"inapp\",\"body\":\"message that is going to create record\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"zoro\"}}",
:headers => {'Authorization'=>'Basic Og=='})
.to_return(:status => 200, :body => web_response_factory('intercom/intercom_post_message'), :headers => {})
end

it 'calls the send_message method in the driver' do
expect_any_instance_of(Integrations::Drivers::Intercom).to receive(:send_message)
post_with user, :notify_subscribers, intercom_params
end

it 'redirects to the error with success message' do
post_with user, :notify_subscribers, intercom_params
expect(response).to redirect_to(error_path(group.id))
expect(flash[:success]).to eq('Message successfully sent!')
end
end

context 'if logged in' do
it 'assigns current_site errors' do
get_with user, :index, params, { epiclogger_website_id: website.id}
expect(assigns(:errors)).to eq([group])
context 'failure' do
it 'redirects to error path with error message' do
stub_request(:post, integration_driver.api_url + 'messages').
with(:body => "{\"message_type\":\"inapp\",\"body\":\"message that is going to create record\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"zoro\"}}",
:headers => {'Authorization'=>'Basic Og=='}).
to_return(:status => 500, :body => "eroare", :headers => {})
post_with user, :notify_subscribers, intercom_params
expect(response).to redirect_to(error_path(group.id))
expect(flash[:error]).to eq('Operation failed!')
end
end
end
end

context 'not logged in' do
let(:params) { { current_issue: issue_error.id } }

Expand All @@ -57,6 +94,17 @@
end
end

describe 'GET #index' do
let(:params) { { current_issue: issue_error.id } }

context 'if logged in' do
it 'assigns current_site errors' do
get_with user, :index, params, { epiclogger_website_id: website.id}
expect(assigns(:errors)).to eq([group])
end
end
end

describe 'GET #show' do
let(:params) { { status: 'resolved', id: group.id, website_id: website.id } }
render_views
Expand Down
Loading

0 comments on commit 1876099

Please sign in to comment.