Commit c23eb4082948322a1b690e0850c09bfc8df81589
1 parent
caef9ed1
Exists in
master
and in
4 other branches
SSH Keys API implemented
Showing
4 changed files
with
106 additions
and
0 deletions
Show diff stats
lib/api.rb
lib/api/entities.rb
| ... | ... | @@ -0,0 +1,44 @@ |
| 1 | +module Gitlab | |
| 2 | + # Keys API | |
| 3 | + class Keys < Grape::API | |
| 4 | + before { authenticate! } | |
| 5 | + resource :keys do | |
| 6 | + # Get currently authenticated user's keys | |
| 7 | + # | |
| 8 | + # Example Request: | |
| 9 | + # GET /keys | |
| 10 | + get do | |
| 11 | + present current_user.keys, with: Entities::Key | |
| 12 | + end | |
| 13 | + # Add new ssh key to currently authenticated user | |
| 14 | + # | |
| 15 | + # Parameters: | |
| 16 | + # key (required) - New SSH Key | |
| 17 | + # title (required) - New SSH Key's title | |
| 18 | + # Example Request: | |
| 19 | + # POST /keys | |
| 20 | + post do | |
| 21 | + key = current_user.keys.new( | |
| 22 | + title: params[:title], | |
| 23 | + key: params[:key] | |
| 24 | + ) | |
| 25 | + if key.save | |
| 26 | + present key, with: Entities::Key | |
| 27 | + else | |
| 28 | + not_found! | |
| 29 | + end | |
| 30 | + end | |
| 31 | + # Delete existed ssh key of currently authenticated user | |
| 32 | + # | |
| 33 | + # Parameters: | |
| 34 | + # id (required) - SSH Key ID | |
| 35 | + # Example Request: | |
| 36 | + # DELETE /keys/:id | |
| 37 | + delete "/:id" do | |
| 38 | + key = current_user.keys.find params[:id] | |
| 39 | + key.delete | |
| 40 | + end | |
| 41 | + end | |
| 42 | + end | |
| 43 | +end | |
| 44 | + | ... | ... |
| ... | ... | @@ -0,0 +1,55 @@ |
| 1 | +require 'spec_helper' | |
| 2 | + | |
| 3 | +describe Gitlab::Keys do | |
| 4 | + include ApiHelpers | |
| 5 | + let(:user) { | |
| 6 | + user = Factory.create :user | |
| 7 | + user.reset_authentication_token! | |
| 8 | + user | |
| 9 | + } | |
| 10 | + let(:key) { Factory.create :key, { user: user}} | |
| 11 | + | |
| 12 | + describe "GET /keys" do | |
| 13 | + context "when unauthenticated" do | |
| 14 | + it "should return authentication error" do | |
| 15 | + get api("/keys") | |
| 16 | + response.status.should == 401 | |
| 17 | + end | |
| 18 | + end | |
| 19 | + context "when authenticated" do | |
| 20 | + it "should return array of ssh keys" do | |
| 21 | + user.keys << key | |
| 22 | + user.save | |
| 23 | + get api("/keys", user) | |
| 24 | + response.status.should == 200 | |
| 25 | + json_response.should be_an Array | |
| 26 | + json_response.first["title"].should == key.title | |
| 27 | + end | |
| 28 | + end | |
| 29 | + end | |
| 30 | + | |
| 31 | + describe "POST /keys" do | |
| 32 | + it "should not create invalid ssh key" do | |
| 33 | + post api("/keys", user), { title: "invalid key" } | |
| 34 | + response.status.should == 404 | |
| 35 | + end | |
| 36 | + it "should create ssh key" do | |
| 37 | + key_attrs = Factory.attributes :key | |
| 38 | + expect { | |
| 39 | + post api("/keys", user), key_attrs | |
| 40 | + }.to change{ user.keys.count }.by(1) | |
| 41 | + end | |
| 42 | + end | |
| 43 | + | |
| 44 | + describe "DELETE /keys/:id" do | |
| 45 | + it "should delete existed key" do | |
| 46 | + user.keys << key | |
| 47 | + user.save | |
| 48 | + expect { | |
| 49 | + delete api("/keys/#{key.id}", user) | |
| 50 | + }.to change{user.keys.count}.by(-1) | |
| 51 | + end | |
| 52 | + end | |
| 53 | + | |
| 54 | +end | |
| 55 | + | ... | ... |