Skip to content

Commit

Permalink
add assign group to VPN API
Browse files Browse the repository at this point in the history
  • Loading branch information
giosakti committed Jan 28, 2019
1 parent ecb706a commit 0b8aeeb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/controllers/api/v1/vpns_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ::Api::V1::VpnsController < ::Api::V1::BaseController
before_action :set_vpn, only: [:assign_group]

def create
if current_user.admin?
@vpn = Vpn.new(vpn_params)
Expand All @@ -10,8 +12,20 @@ def create
end
end

def assign_group
if current_user.admin?
@vpn.groups.delete_all
@vpn.groups << Group.where(id: params[:group_id]).first
render json: { status: 'group assigned' }, status: :ok
end
end

private

def set_vpn
@vpn = Vpn.find(params[:id])
end

def vpn_params
params.require(:vpn).permit(:name, :host_name, :ip_address)
end
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@
post 'users/profile' => 'users#update', format: :json, :constraints => { format: 'json' }

resources :groups, only: [:create], format: :json
resources :vpns, only: [:create], format: :json
resources :vpns, only: [:create], format: :json do
member do
post 'assign_group'
end
end
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/controllers/api/v1/vpns_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@
end
end
end

describe 'Assign Group to VPN' do
it 'should replace existing vpn group with new group' do
vpn = create(:vpn)
group_1 = create(:group)
group_2 = create(:group)
vpn.groups << group_1
vpn.groups << group_2
group_3 = create(:group)
post :assign_group, params: {access_token: @token, id: vpn.id, group_id: group_3.id}
expect(vpn.groups.count).to eq 1
expect(vpn.groups.first).to eq group_3
end
end
end

describe 'Unauthenticated' do
Expand Down

0 comments on commit 0b8aeeb

Please sign in to comment.