Skip to content

Commit

Permalink
refactor pivotal_tracke angular controller to use integrations_factor…
Browse files Browse the repository at this point in the history
…y and new unified create action frfom api
  • Loading branch information
Bartlomiej Skwira committed Feb 9, 2014
1 parent 9e97770 commit 236cb37
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ KarmaTracker.controller "GitHubIntegrationsController",['$scope', 'Integration',
$scope.newIntegration = { username: null, password: null }
$scope.addFormShown = false
$scope.errors = {}
$scope.tokenName = 'token'
integrationService = new Integration

$scope.updateIntegrations = ->
integrationService.query('git_hub').$promise.then (result) ->
$scope.integrations = result

$scope.remove = (integration_id) ->
if confirm("are you sure to remove github integration?")
if confirm("Are you sure you want to remove Git Hub integration?")
integrationService.remove(integration_id).$promise.then (result) ->
index = $scope.integrations.map((integration) ->
integration.git_hub.id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,77 +1,60 @@
KarmaTracker.controller "PivotalTrackerIntegrationsController", ($scope, $http, $cookieStore, $location) ->
KarmaTracker.controller "PivotalTrackerIntegrationsController", ['$scope', 'Integration', ($scope, Integration) ->
$scope.integrations = []
$scope.newIntegration = { api_key: null, email: null, password: null }
$scope.addFormShown = false
$scope.errors = {}
$scope.tokenName = 'token'
integrationService = new Integration


$scope.updateIntegrations = ->
$http.get(
'/api/v1/integrations?token='+$cookieStore.get($scope.tokenName)+'&service=pivotal_tracker'
).success((data, status, headers, config) ->
$scope.integrations = data
).error((data, status, headers, config) ->
)
integrationService.query('pivotal_tracker').$promise.then (result) ->
$scope.integrations = result

$scope.remove = (id) ->
answer = confirm("Are you sure to remove Pivotal Tracker Integration?")
if answer
$http.delete(
'/api/v1/integrations/'+id+'?token='+$cookieStore.get($scope.tokenName)+'&service=pivotal_tracker'
).success((data, status, headers, config) ->
$scope.updateIntegrations()
).error((data, status, headers, config) ->
)
$scope.remove = (integration_id) ->
if confirm("Are you sure you want to remove Pivotal Tracker integration?")
integrationService.remove(integration_id).$promise.then (result) ->
index = $scope.integrations.map((integration) ->
integration.pivotal_tracker.id
).indexOf(integration_id)
$scope.integrations.splice(index, 1) if index > -1

$scope.formLooksValid = () ->
valid = true
$scope.errors = {}

unless $scope.newIntegration.api_key? or $scope.newIntegration.api_key == ''
for field in ["email", "password"]
for field in ["username", "password"]
unless $scope.newIntegration[field]? and $scope.newIntegration[field] != ''
$scope.errors[field] = "can't be blank"
valid = false

valid

$scope.add = () ->
$scope.add = ->
if $scope.formLooksValid()
if $scope.newIntegration.api_key? and $scope.newIntegration.api_key != ''
$http.post(
'/api/v1/integrations/pivotal_tracker?token='+$cookieStore.get($scope.tokenName)+'&integration[api_key]='+$scope.newIntegration.api_key
).success((data, status, headers, config) ->
$scope.cleanForm()
$scope.openAddForm()
$scope.updateIntegrations()
).error((data, status, headers, config) ->
$scope.errors['api_key'] = data.pivotal_tracker.errors.api_key[0]
)
else
$http.post(
'/api/v1/integrations/pivotal_tracker?token='+$cookieStore.get($scope.tokenName)+'&integration[email]='+$scope.newIntegration.email+'&integration[password]='+$scope.newIntegration.password
).success((data, status, headers, config) ->
$scope.cleanForm()
$scope.newIntegration['type'] = 'pivotal_tracker'
integrationService.save($scope.newIntegration).$promise
.then (result) ->
$scope.openAddForm()
$scope.updateIntegrations()
).error((data, status, headers, config) ->
$scope.newIntegration.email = ''
$scope.newIntegration.password = ''
if data.pivotal_tracker.errors.api_key?
$scope.errors.password = data.pivotal_tracker.errors.api_key[0]
$scope.integrations.push result
.catch (response) ->
data = response.data.pivotal_tracker
if data.errors.api_key?
$scope.errors.api_key = data.errors.api_key[0]
$scope.errors.password = data.errors.api_key[0]
else
$scope.errors.password = data.pivotal_tracker.errors.password[0]
)
$scope.errors.username_token = data.errors.username[0] if data.errors.username?
$scope.errors.password = data.errors.password[0] if data.errors.password?

$scope.openAddForm = () ->
$scope.openAddForm = ->
$scope.addFormShown = !$scope.addFormShown
$scope.cleanForm()

$scope.cleanForm = () ->
$scope.cleanForm = ->
$scope.newIntegration.email = ''
$scope.newIntegration.password = ''
$scope.newIntegration.api_key =''
$scope.errors = {}


$scope.updateIntegrations()
]
18 changes: 9 additions & 9 deletions app/controllers/api/v1/integrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ def show
# => {"pivotal_tracker": {"email": "[email protected]", "password": "wrong_password",
# "errors": { "password": ["does not match email"] }}}
#
def pivotal_tracker
options = (params[:integration] || {}).merge({user_id: @current_user.id})
@integration = IntegrationsFactory.new(PivotalTrackerIntegration.new, options).create
if @integration.save
render '_show'
else
render '_show', status: 422
end
end
# def pivotal_tracker
#options = (params[:integration] || {}).merge({user_id: @current_user.id})
#@integration = IntegrationsFactory.new(PivotalTrackerIntegration.new, options).create
#if @integration.save
#render '_show'
#else
#render '_show', status: 422
#end
#end

def create
options = (params[:integration].reject{ |key, value| value.empty?} || {}).merge({user_id: @current_user.id})
Expand Down
4 changes: 2 additions & 2 deletions spec/api/integrations_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
# POST /api/v1/integrations/pivotal_tracker
it "should be able to add PT integration for given user with provided token" do
user = create :user
json = api_post "integrations/pivotal_tracker", {token: ApiKey.last.token, integration:{ api_key: 'correct_token'}}
json = api_post "integrations/", {token: ApiKey.last.token, integration:{ api_key: 'correct_token', type: 'pivotal_tracker'}}
response.status.should == 200
json.has_key?('pivotal_tracker').should be_true

Expand All @@ -106,7 +106,7 @@
# POST /api/v1/integrations/pivotal_tracker
it 'should add error messages to response when adding PT integration fails' do
create :user
json = api_post "integrations/pivotal_tracker", {token: ApiKey.last.token, integration: {username: 'wrong_email', password: 'wrong_password'}}
json = api_post "integrations/", {token: ApiKey.last.token, integration: {username: 'wrong_email', password: 'wrong_password', type: 'pivotal_tracker'}}

response.status.should == 422
Integration.count.should == 0
Expand Down
4 changes: 2 additions & 2 deletions spec/features/users/integrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
page.should have_content 'Pivotal Tracker'
end

scenario 'adds and removes new Pivotal Tracker integration with credentials', driver: :selenium do
scenario 'adds and removes new Pivotal Tracker integration with credentials' do
click_link 'add_new_pt'
within 'div#ptform' do
fill_in 'email', :with => 'correct_email@example.com'
fill_in 'email', :with => 'correct_username@example.com'
fill_in 'password', :with => 'correct_password'
click_button "Add new integration"
end
Expand Down
5 changes: 3 additions & 2 deletions spec/models/integrations_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
describe 'IntegrationsFactory' do

before :all do
@attrs = {email: 'correct_email', password: 'correct_password'}
@attrs = {username: 'correct_username', password: 'correct_password'}
end

it 'should return an integration when correct params were provided' do
factory = IntegrationsFactory.new(PivotalTrackerIntegration.new, @attrs)
options = @attrs.merge({ type: 'pivotal_tracker' })
factory = IntegrationsFactory.new(IntegrationsFactory.construct_integration(options), @attrs)
integration = factory.create
integration.type.should == "PivotalTrackerIntegration"
end
Expand Down
6 changes: 3 additions & 3 deletions spec/models/pivotal_tracker_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
it 'should not save integration when incorrect credentials were provided' do
PivotalTrackerIntegration.count.should == 0
pi = PivotalTrackerIntegration.new
pi.email = 'wrong_email'
pi.username = 'wrong_email'
pi.password = 'wrong_password'
pi.save
PivotalTrackerIntegration.count.should == 0
Expand All @@ -22,15 +22,15 @@
PivotalTrackerIntegration.count.should == 0
pi = PivotalTrackerIntegration.new
pi.user = FactoryGirl.create :user
pi.email = 'correct_email@example.com'
pi.username = 'correct_username@example.com'
pi.password = 'correct_password'
pi.save
PivotalTrackerIntegration.count.should == 1
PivotalTrackerIntegration.last.api_key.should == '377ec0d3698e5f80c4e108fb26d7a105'

pi = PivotalTrackerIntegration.new
pi.user = FactoryGirl.create :user
pi.email = 'correct_email'
pi.username = 'correct_username'
pi.password = 'correct_password'
pi.save.should be_false
pi.errors[:password].should be_present
Expand Down

0 comments on commit 236cb37

Please sign in to comment.