Commit 3bee09d408b46ebe646c58aac113f490d481394b

create keys by xml

Commit diff

app/controllers/keys_controller.rb

 
33
44 def index
55 @ssh_keys = current_user.ssh_keys
6 respond_to do |format|
7 format.html
8 format.xml { render :xml => @ssh_keys }
9 end
610 end
711
812 def new
913 @ssh_key = current_user.ssh_keys.new
1014 end
11
15 # respond_to do |format|
16 # if @event.save
17 # flash[:notice] = 'Event was successfully created.'
18 # format.html { redirect_to(@event) }
19 # format.xml { render :xml => @event, :status => :created, :location => @event }
20 # else
21 # format.html { render :action => "new" }
22 # format.xml { render :xml => @event.errors, :status => :unprocessable_entity }
23 # end
1224 def create
1325 @ssh_key = current_user.ssh_keys.new
1426 @ssh_key.key = params[:ssh_key][:key]
15 if @ssh_key.save
16 flash[:notice] = "Key added"
17 redirect_to account_path
18 else
19 render :action => "new"
27
28 respond_to do |format|
29 if @ssh_key.save
30 flash[:notice] = "Key added"
31 format.html { redirect_to account_path }
32 format.xml { render :xml => @ssh_key, :status => :created, :location => account_key_path(@ssh_key) }
33 else
34 format.html { render :action => "new" }
35 format.xml { render :xml => @ssh_key.errors, :status => :unprocessable_entity }
36 end
2037 end
2138 end
2239
2340 def show
2441 @ssh_key = current_user.ssh_keys.find(params[:id])
42
43 respond_to do |format|
44 format.html
45 format.xml { render :xml => @ssh_key }
46 end
2547 end
2648
2749 # can't update keys since yet we'd have to to search/replace through
toggle raw diff

spec/controllers/keys_controller_spec.rb

 
2828 end
2929end
3030
31describe KeysController, "index.xml" do
32
33 before(:each) do
34 authorize_as :johan
35 end
36
37 def do_get
38 @request.env["HTTP_ACCEPT"] = "application/xml"
39 get :index
40 end
41
42 it "requires login" do
43 authorize_as(nil)
44 do_get
45 response.code.to_i.should == 401
46 end
47
48 it "GET account/keys is successful" do
49 do_get
50 response.should be_success
51 end
52
53 it "scopes to the current_users keys" do
54 do_get
55 response.body.should == users(:johan).ssh_keys.to_xml
56 end
57end
58
3159describe KeysController, "new" do
3260
3361 before(:each) do
8383 end
8484end
8585
86describe KeysController, "create" do
87
88 before(:each) do
89 login_as :johan
90 end
91
92 def valid_key
86module KeyStubs
87 def valid_key
9388 <<-EOS
9489ssh-rsa bXljYWtkZHlpemltd21vY2NqdGJnaHN2bXFjdG9zbXplaGlpZnZ0a3VyZWFz
9590c2dkanB4aXNxamxieGVib3l6Z3hmb2ZxZW15Y2FrZGR5aXppbXdtb2NjanRi
9691Z2hzdm1xY3Rvc216ZWhpaWZ2dGt1cmVhc3NnZGpweGlzcWpsYnhlYm95emd4
9792Zm9mcWU= foo@example.com
9893EOS
99 end
94 end
95
96 def invalid_key
97 "ooger booger wooger@burger"
98 end
99end
100
101describe KeysController, "create" do
102 include KeyStubs
100103
101 def invalid_key
102 "ooger booger wooger@burger"
104 before(:each) do
105 login_as :johan
103106 end
104107
105108 def do_post(opts={})
125125 response.should be_redirect
126126 end
127127end
128
129describe KeysController, "create.xml" do
130 include KeyStubs
131
132 before(:each) do
133 authorize_as :johan
134 end
135
136 def do_post(opts={})
137 @request.env["HTTP_ACCEPT"] = "application/xml"
138 post :create, :ssh_key => {:key => valid_key}.merge(opts)
139 end
140
141 it "should require login" do
142 authorize_as(nil)
143 do_post
144 response.code.to_i.should == 401
145 end
146
147 it "scopes to the current_user" do
148 do_post
149 assigns[:ssh_key].user_id.should == users(:johan).id
150 end
151
152 it "POST account/keys/create is successful" do
153 do_post
154 response.code.to_i.should == 201
155 end
156end
toggle raw diff