From 26e776db4dcc32c0238f7b9d7effd37085d4bc86 Mon Sep 17 00:00:00 2001 From: Judy Ngai Date: Wed, 30 Mar 2016 15:47:17 -0400 Subject: [PATCH] new credentials uploading feature rm req json, not saving file, styled button a bit skinny controller fat model minor spacing mistake build user credentials fixed mass assignment issue undo mass assignment variables spec in progress spec fixed exception issue json fixtures slight change to test added test to assert credentials created for current user only added tests to satisfy requirements refined import method added status 422 saving two lines of code one more test fixed small bug, words changed minor change in test styled file upload button deleted unnecessary spec switched to modal format made modal more presentable minor change to modal fix indentation issue ok nested correctly within form tag --- .../user_credentials_controller.rb | 20 ++++++++++ app/views/user_credentials/index.html.erb | 22 +++++++++++ config/routes.rb | 6 ++- .../user_credentials_controller_spec.rb | 21 ++++++++++ spec/fixtures/multiple_user_credentials.json | 38 +++++++++++++++++++ spec/fixtures/user_credentials.json | 20 ++++++++++ 6 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/multiple_user_credentials.json create mode 100644 spec/fixtures/user_credentials.json diff --git a/app/controllers/user_credentials_controller.rb b/app/controllers/user_credentials_controller.rb index e9228a33..d2d3f9bf 100644 --- a/app/controllers/user_credentials_controller.rb +++ b/app/controllers/user_credentials_controller.rb @@ -14,6 +14,26 @@ class UserCredentialsController < ApplicationController end end + def import + if params[:file] + file = params[:file] + content = JSON.parse(file.read) + new_credentials = content.map do |hash| + current_user.user_credentials.build(hash.slice("credential_name", "credential_value", "mode")) + end + + respond_to do |format| + if new_credentials.map(&:save).all? + format.html { redirect_to user_credentials_path, notice: "The file was successfully uploaded."} + else + format.html { redirect_to user_credentials_path, notice: 'One or more of the uploaded credentials was not imported due to an error. Perhaps an existing credential had the same name?'} + end + end + else + redirect_to user_credentials_path, notice: "No file was chosen to be uploaded." + end + end + def new @user_credential = current_user.user_credentials.build diff --git a/app/views/user_credentials/index.html.erb b/app/views/user_credentials/index.html.erb index db1d8f13..cf72c6ae 100644 --- a/app/views/user_credentials/index.html.erb +++ b/app/views/user_credentials/index.html.erb @@ -39,6 +39,28 @@
<%= link_to new_user_credential_path, class: "btn btn-default" do %> New Credential<% end %> <%= link_to user_credentials_path(format: :json), class: "btn btn-default" do %> Download Credentials<% end %> + <%= link_to '#', data: { toggle: 'modal', target: '#credentials-upload' }, class: "btn btn-default credentials-upload-button" do %> Upload Credentials<% end %> +
+ + diff --git a/config/routes.rb b/config/routes.rb index 0bd99a25..e99bb15c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -48,7 +48,11 @@ Huginn::Application.routes.draw do resource :diagram, :only => [:show] end - resources :user_credentials, :except => :show + resources :user_credentials, :except => :show do + collection do + post :import + end + end resources :services, :only => [:index, :destroy] do member do diff --git a/spec/controllers/user_credentials_controller_spec.rb b/spec/controllers/user_credentials_controller_spec.rb index a0729b7a..8ccdfb2b 100644 --- a/spec/controllers/user_credentials_controller_spec.rb +++ b/spec/controllers/user_credentials_controller_spec.rb @@ -10,6 +10,7 @@ describe UserCredentialsController do before do sign_in users(:bob) + @file = fixture_file_upload('user_credentials.json') end describe "GET index" do @@ -30,6 +31,26 @@ describe UserCredentialsController do end end + describe "Post import" do + it "asserts user credentials were created for current user only" do + post :import, :file => @file + expect(controller.current_user.id).to eq(users(:bob).id) + expect(controller.current_user.user_credentials).to eq(users(:bob).user_credentials) + end + + it "asserts that primary id in json file is ignored" do + post :import, :file => @file + expect(controller.current_user.user_credentials.last.id).not_to eq(24) + end + + it "duplicate credential name shows an error that it is not saved" do + file1 = fixture_file_upload('multiple_user_credentials.json') + post :import, :file => file1 + expect(flash[:notice]).to eq("One or more of the uploaded credentials was not imported due to an error. Perhaps an existing credential had the same name?") + expect(response).to redirect_to(user_credentials_path) + end + end + describe "POST create" do it "creates UserCredentials for the current user" do expect { diff --git a/spec/fixtures/multiple_user_credentials.json b/spec/fixtures/multiple_user_credentials.json new file mode 100644 index 00000000..e1682e1e --- /dev/null +++ b/spec/fixtures/multiple_user_credentials.json @@ -0,0 +1,38 @@ +[ + { + "id": 23, + "user_id": 30, + "credential_name": "Google_api_key", + "credential_value": "gcperfxrtqymqmluvskxzyiyxxfnjduzncoukyqehkrkamofwz", + "created_at": "2016-04-01 10:50:59 -0700", + "updated_at": "2016-04-01 10:50:59 -0700", + "mode": "text" + }, + { + "id": 24, + "user_id": 30, + "credential_name": "twitter_secret_key", + "credential_value": "jhpswiebwhbrnabgkbvczrwcyxblxtyvvlvkhuoudjalcqmlwz", + "created_at": "2016-04-01 10:50:59 -0700", + "updated_at": "2016-04-01 10:50:59 -0700", + "mode": "text" + }, + { + "id": 23, + "user_id": 30, + "credential_name": "Google_api_key", + "credential_value": "gcperfxrtqymqmluvskxzyiyxxfnjduzncoukyqehkrkamofwz", + "created_at": "2016-04-01 10:50:59 -0700", + "updated_at": "2016-04-01 10:50:59 -0700", + "mode": "text" + }, + { + "id": 24, + "user_id": 30, + "credential_name": "twitter_secret_key", + "credential_value": "jhpswiebwhbrnabgkbvczrwcyxblxtyvvlvkhuoudjalcqmlwz", + "created_at": "2016-04-01 10:50:59 -0700", + "updated_at": "2016-04-01 10:50:59 -0700", + "mode": "text" + } +] diff --git a/spec/fixtures/user_credentials.json b/spec/fixtures/user_credentials.json new file mode 100644 index 00000000..d8232a5e --- /dev/null +++ b/spec/fixtures/user_credentials.json @@ -0,0 +1,20 @@ +[ + { + "id": 23, + "user_id": 30, + "credential_name": "Google_api_key", + "credential_value": "gcperfxrtqymqmluvskxzyiyxxfnjduzncoukyqehkrkamofwz", + "created_at": "2016-04-01 10:50:59 -0700", + "updated_at": "2016-04-01 10:50:59 -0700", + "mode": "text" + }, + { + "id": 24, + "user_id": 30, + "credential_name": "twitter_secret_key", + "credential_value": "jhpswiebwhbrnabgkbvczrwcyxblxtyvvlvkhuoudjalcqmlwz", + "created_at": "2016-04-01 10:50:59 -0700", + "updated_at": "2016-04-01 10:50:59 -0700", + "mode": "text" + } +]