Commit 697bd4ec by John Doe

finished user group endpoints

parent cc0192b2
...@@ -50,7 +50,14 @@ class UserGroupsController < ApplicationController ...@@ -50,7 +50,14 @@ class UserGroupsController < ApplicationController
# PATCH/PUT /user_groups/1.json # PATCH/PUT /user_groups/1.json
def update def update
# TODO @service = StubService.new
if @user_group.update(user_group_params)
@service.add_notice('updated group')
render :show, status: :ok
else
@service.errors = @user_group.errors.full_messages
render :show, status: :unprocessable_entity
end
end end
# DELETE /user_groups/1.json # DELETE /user_groups/1.json
......
...@@ -7,7 +7,7 @@ Rails.application.routes.draw do ...@@ -7,7 +7,7 @@ Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'auth' mount_devise_token_auth_for 'User', at: 'auth'
resources :users, only: [:index, :create, :destroy] resources :users, only: [:index, :create, :destroy]
resources :user_groups, only: [:index, :create, :destroy] do resources :user_groups, only: [:index, :update, :create, :destroy] do
member do member do
put 'add_member' put 'add_member'
put 'remove_member' put 'remove_member'
......
...@@ -221,6 +221,54 @@ end ...@@ -221,6 +221,54 @@ end
expect(UserGroup.exists?(group.id)).to be true expect(UserGroup.exists?(group.id)).to be true
end end
end end
end
describe 'PUT update' do
context 'with owner' do
it 'updates the group' do
@auth_headers = owner.create_new_auth_token
put "/user_groups/#{group.id}.json",
params: {name: 'new', description: 'changed'},
headers: @auth_headers
expect(response).to have_http_status(:ok)
expect(response).to have_notice_message
expect(group.reload.name).to eq('new')
expect(group.description).to eq('changed')
#check to make sure JSON renders the members
body = JSON.parse(response.body)
expect(body['data']['members'].count).to eq group.users.count
end
it 'returns error if unsuccesful' do
@auth_headers = owner.create_new_auth_token
orig_name = group.name
# name cannot be blank
put "/user_groups/#{group.id}.json",
params: {name: '', description: 'changed'},
headers: @auth_headers
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_error_message
expect(group.reload.name).to eq(orig_name)
end
end
context 'with anybody else' do
it 'returns unauthorized' do
@auth_headers = member1.create_new_auth_token
orig_name = group.name
put "/user_groups/#{group.id}.json",
params: {name: 'new', description: 'changed'},
headers: @auth_headers
expect(response).to have_http_status(:unauthorized)
expect(group.reload.name).to eq orig_name
end
end
context 'without sign-in' do
it 'returns unauthorized' do
orig_name = group.name
put "/user_groups/#{group.id}.json",
params: {name: 'new', description: 'changed'}
expect(response).to have_http_status(:unauthorized)
expect(group.reload.name).to eq orig_name
end
end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment