-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from gate-sso/issue-#138
Issue #138 - Create APIs for managing groups and VPNs
- Loading branch information
Showing
10 changed files
with
179 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class ::Api::V1::GroupsController < ::Api::V1::BaseController | ||
def create | ||
if current_user.admin? | ||
@group = Group.new(group_params) | ||
if @group.save | ||
render json: { status: 'created' }, status: :ok | ||
else | ||
render json: { status: 'error' }, status: :unprocessable_entity | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def group_params | ||
params.require(:group).permit(:name) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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) | ||
if @vpn.save | ||
render json: { status: 'created' }, status: :ok | ||
else | ||
render json: { status: 'error' }, status: :unprocessable_entity | ||
end | ||
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 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
spec/controllers/api_controller_spec.rb → ...controllers/api/v1/api_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe ::Api::V1::GroupsController, type: :controller do | ||
let(:valid_attributes) { | ||
{name: 'jumbo'} | ||
} | ||
|
||
before(:each) do | ||
@user = build(:user) | ||
@user.access_token = build(:access_token) | ||
@user.save | ||
@token = @user.access_token.token | ||
end | ||
|
||
describe 'Authenticated' do | ||
describe 'Create Group' do | ||
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 | ||
end | ||
end | ||
end | ||
|
||
describe 'Unauthenticated' do | ||
it 'gives 401 when access token is invalid' do | ||
post :create, params: {group: valid_attributes, access_token: 'foo'} | ||
expect(response.status).to eq(401) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe ::Api::V1::VpnsController, type: :controller do | ||
let(:valid_attributes) { | ||
{name: 'jumbo'} | ||
} | ||
|
||
before(:each) do | ||
@user = build(:user) | ||
@user.access_token = build(:access_token) | ||
@user.save | ||
@token = @user.access_token.token | ||
end | ||
|
||
describe 'Authenticated' do | ||
describe 'Create Vpn' do | ||
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 | ||
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 | ||
it 'gives 401 when access token is invalid' do | ||
post :create, params: {vpn: valid_attributes, access_token: 'foo'} | ||
expect(response.status).to eq(401) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters