Skip to content

Commit

Permalink
group and VPN creation API should return created object response
Browse files Browse the repository at this point in the history
  • Loading branch information
giosakti committed Jan 28, 2019
1 parent b119e45 commit a4a032f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
5 changes: 4 additions & 1 deletion app/controllers/api/v1/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ def create
if current_user.admin?
@group = Group.new(group_params)
if @group.save
render json: { status: 'created' }, status: :ok
render json: {
id: @group.id,
name: @group.name,
}, status: :ok
else
render json: { status: 'error' }, status: :unprocessable_entity
end
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/api/v1/vpns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ def create
if current_user.admin?
@vpn = Vpn.new(vpn_params)
if @vpn.save
render json: { status: 'created' }, status: :ok
render json: {
id: @vpn.id,
name: @vpn.name,
host_name: @vpn.host_name,
ip_address: @vpn.ip_address,
}, status: :ok
else
render json: { status: 'error' }, status: :unprocessable_entity
end
Expand Down
10 changes: 9 additions & 1 deletion spec/controllers/api/v1/groups_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@
context 'with valid_attributes' do
it 'should create groups' do
post :create, params: {group: valid_attributes, access_token: @token}
expect(response.status).to eq(200)
group = Group.where(name: valid_attributes[:name]).first
expect(group.blank?).to eq(false)
expect(group.name).to eq(valid_attributes[:name])
end

it 'should return proper response' do
post :create, params: {group: valid_attributes, access_token: @token}
expect(response.status).to eq(200)
group = Group.where(name: valid_attributes[:name]).first
obj = JSON.parse(response.body)
expect(obj['id']).to eq group.id
expect(obj['name']).to eq group.name
end
end
end
end
Expand Down
12 changes: 11 additions & 1 deletion spec/controllers/api/v1/vpns_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@
context 'with valid_attributes' do
it 'should create vpns' do
post :create, params: {vpn: valid_attributes, access_token: @token}
expect(response.status).to eq(200)
vpn = Vpn.where(name: valid_attributes[:name]).first
expect(vpn.blank?).to eq(false)
expect(vpn.name).to eq(valid_attributes[:name])
end

it 'should return proper response' do
post :create, params: {vpn: valid_attributes, access_token: @token}
expect(response.status).to eq(200)
vpn = Vpn.where(name: valid_attributes[:name]).first
obj = JSON.parse(response.body)
expect(obj['id']).to eq vpn.id
expect(obj['name']).to eq vpn.name
expect(obj['host_name']).to eq vpn.host_name
expect(obj['ip_address']).to eq vpn.ip_address
end
end
end

Expand Down

0 comments on commit a4a032f

Please sign in to comment.