Skip to content

Commit

Permalink
gocd#430 - API for update key.
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindsv committed Aug 12, 2014
1 parent d402f5d commit 99dbdbd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
Expand Up @@ -35,4 +35,16 @@ public List<ValidationError> validate(String name, String hostname, String usern
public SshKey addKey(String name, String hostname, String username, String key, String resources) {
throw new RuntimeException("Not implemented yet!");
}

public boolean hasKey(String id) {
throw new RuntimeException("Not implemented yet!");
}

public List<ValidationError> validateUpdate(String id, String name, String hostname, String username, String resources) {
throw new RuntimeException("Not implemented yet!");
}

public SshKey updateKey(String id, String name, String hostname, String username, String resources) {
throw new RuntimeException("Not implemented yet!");
}
}
Expand Up @@ -31,6 +31,16 @@ def create
render :json => convert_to_hash(added_key)
end

def update
render :json => {:errors => [{:key => 'key_not_found', :message => "Cannot find key with ID: #{params[:id]}"}]}, :status => 422 and return unless ssh_keys_service.hasKey(params[:id])

errors = ssh_keys_service.validateUpdate(params[:id], params[:name], params[:hostname], params[:username], params[:resources])
render :json => {:errors => errors.collect {|error| {:key => error.key, :message => error.value}}}, :status => 422 and return unless errors.empty?

updated_key = ssh_keys_service.updateKey(params[:id], params[:name], params[:hostname], params[:username], params[:resources])
render :json => convert_to_hash(updated_key)
end

private

def convert_to_hash(ssh_key)
Expand Down
Expand Up @@ -18,8 +18,10 @@

describe Admin::SshKeysController do
describe :route do
it "should resolve index" do
it "should resolve" do
expect({:get => "/admin/ssh_keys"}).to route_to(:controller => "admin/ssh_keys", :action => "index")
expect({:post => "/admin/ssh_keys"}).to route_to(:controller => "admin/ssh_keys", :action => "create")
expect({:put => "/admin/ssh_keys/ID1"}).to route_to(:controller => "admin/ssh_keys", :action => "update", :id => "ID1")
end
end

Expand Down Expand Up @@ -90,6 +92,59 @@
end
end

describe :update do
before :each do
@ssh_keys_service = stub_service(:ssh_keys_service)
end

it 'should fail if ssh key cannot be found' do
@ssh_keys_service.should_receive(:hasKey).with("ID1") { false }
@ssh_keys_service.should_not_receive(:validateUpdate)
@ssh_keys_service.should_not_receive(:updateKey)

put :update, {:id => "ID1", :name => "NAME2", :hostname => "HOSTNAME2", :username => "USERNAME2", :resources => "RESOURCES2"}

output = JSON.parse(response.body)

expect(response.status).to eq(422)
expect(output["errors"].size).to eq(1)
expect(output["errors"][0]).to eq({"key" => "key_not_found", "message" => "Cannot find key with ID: ID1"})
end

it 'should fail if ssh key can be found, but data is invalid' do
errors = [ValidationError.new("name", "Is wrong"), ValidationError.new("hostname", "Is also wrong")]

@ssh_keys_service.should_receive(:hasKey).with("ID1") { true }
@ssh_keys_service.should_receive(:validateUpdate).with("ID1", "NAME2", "HOSTNAME2", "USERNAME2", "RESOURCES2") { errors }
@ssh_keys_service.should_not_receive(:updateKey)

put :update, {:id => "ID1", :name => "NAME2", :hostname => "HOSTNAME2", :username => "USERNAME2", :resources => "RESOURCES2"}

output = JSON.parse(response.body)

expect(response.status).to eq(422)
expect(output["errors"].size).to eq(2)
expect(output["errors"][0]).to eq({'key' => 'name', 'message' => 'Is wrong'})
expect(output["errors"][1]).to eq({'key' => 'hostname', 'message' => 'Is also wrong'})
end

it 'should succeed if ssh key can be found, and data is valid' do
updated_key = SshKeyMother.key "ID1", "NAME2", "HOSTNAME2", "USERNAME2", "KEY2", "RESOURCES2"

@ssh_keys_service.should_receive(:hasKey).with("ID1") { true }
@ssh_keys_service.should_receive(:validateUpdate).with("ID1", "NAME2", "HOSTNAME2", "USERNAME2", "RESOURCES2") { [] }
@ssh_keys_service.should_receive(:updateKey).with("ID1", "NAME2", "HOSTNAME2", "USERNAME2", "RESOURCES2") { updated_key }

put :update, {:id => "ID1", :name => "NAME2", :hostname => "HOSTNAME2", :username => "USERNAME2", :resources => "RESOURCES2"}

output = JSON.parse(response.body)

expect(response.status).to eq(200)
assert_key output, "ID1", "NAME2", "HOSTNAME2", "USERNAME2", "RESOURCES2"
assert_no_private_key_info_in output
end
end

def assert_key key, expected_id, expected_name, expected_hostname, expected_username, expected_resources
expect(key["id"]).to eq(expected_id)
expect(key["name"]).to eq(expected_name)
Expand Down

0 comments on commit 99dbdbd

Please sign in to comment.