Merge pull request #1394 from Jngai/uploadcredentialsfeature

new credentials uploading feature
This commit is contained in:
Andrew Cantino 2016-04-21 20:13:47 -07:00
commit 491dace4a0
6 changed files with 126 additions and 1 deletions

View file

@ -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

View file

@ -39,6 +39,28 @@
<div class="btn-group">
<%= link_to new_user_credential_path, class: "btn btn-default" do %><span class="glyphicon glyphicon-plus"></span> New Credential<% end %>
<%= link_to user_credentials_path(format: :json), class: "btn btn-default" do %><span class="glyphicon glyphicon-cloud-download"></span> Download Credentials<% end %>
<%= link_to '#', data: { toggle: 'modal', target: '#credentials-upload' }, class: "btn btn-default credentials-upload-button" do %><span class="glyphicon glyphicon-upload"></span> Upload Credentials<% end %>
</div>
<div id="credentials-upload" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<%= form_tag import_user_credentials_path, multipart: true do %>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<h4 class="modal-title">Upload Credentials</h4>
</div>
<div class="modal-body">
<p>Upload a credentials file that you have previously exported from a Huginn instance.</p>
<%= file_field_tag :file, class: 'form-control' %>
</div>
<div class="modal-footer">
<%= button_tag 'Cancel', class: 'btn btn-default', 'data-dismiss' => 'modal' %>
<%= submit_tag 'Upload', class: 'btn btn-primary' %>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>

View file

@ -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

View file

@ -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 {

View file

@ -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"
}
]

20
spec/fixtures/user_credentials.json vendored Normal file
View file

@ -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"
}
]