adds specs for team controller actions

This commit is contained in:
zmagod 2017-08-30 16:18:21 +02:00
parent ee054f91b8
commit 1325732b0c
9 changed files with 177 additions and 25 deletions

View file

@ -9,20 +9,20 @@ module ClientApi
end
def details
team_service = ClientApi::TeamsService.new(id: params[:team_id],
team_service = ClientApi::TeamsService.new(team_id: params[:team_id],
current_user: current_user)
success_response('/client_api/teams/details',
team_service.team_page_details_data)
rescue MissingTeamError
rescue ClientApi::CustomTeamError
error_response
end
def change_team
team_service = ClientApi::TeamsService.new(id: params[:team_id],
team_service = ClientApi::TeamsService.new(team_id: params[:team_id],
current_user: current_user)
team_service.change_current_team!
success_response('/client_api/teams/index', team_service.teams_data)
rescue MissingTeamError
rescue ClientApi::CustomTeamError
error_response
end
@ -41,7 +41,7 @@ module ClientApi
def error_response
respond_to do |format|
format.json do
render json: { message: 'Bad boy!' }, status: :bad_request
render json: { message: 'Bad boy!' }, status: :unprocessable_entity
end
end
end

View file

@ -36,7 +36,7 @@ export function getTeamsList() {
export function changeTeam(teamId) {
return dispatch => {
axios
.post(CHANGE_TEAM_PATH, { teamId }, { withCredentials: true })
.post(CHANGE_TEAM_PATH, { team_id: 44 }, { withCredentials: true })
.then(response => {
const teams = response.data.teams.collection;
dispatch(addTeamsData(teams));

View file

@ -1,23 +1,27 @@
module ClientApi
class TeamsService
MissingTeamError = Class.new(StandardError)
def initialize(arg)
team_id = arg.fetch(:team_id) { raise MissingTeamError }
team_id = arg.fetch(:team_id) { raise ClientApi::CustomTeamError }
@team = Team.find_by_id(team_id)
@current_user = arg.fetch(:current_user) { raise MissingTeamError }
raise MissingTeamError unless @current_user.teams.include? @team
@user = arg.fetch(:current_user) { raise ClientApi::CustomTeamError }
raise ClientApi::CustomTeamError unless @user.teams.include? @team
end
def change_current_team!
@current_user.update_attribute(:current_team_id, @team.id)
@user.update_attribute(:current_team_id, @team.id)
end
def team_page_details_data
{ team: @team, users: @team.users.eager_load(:user_teams) }
team_users = UserTeam.includes(:user)
.references(:user)
.where(team: @team)
.distinct
{ team: @team, team_users: team_users }
end
def teams_data
{ teams: @current_user.teams_data }
{ teams: @user.teams_data }
end
end
CustomTeamError = Class.new(StandardError)
end

View file

@ -1,15 +1,18 @@
json.team_details do
json.id team.id
json.created_at team.created_at
json.created_by team.created_by
json.created_by "#{team.created_by.full_name} (#{team.created_by.email})"
json.space_taken team.space_taken
json.description team.description
json.users users do |user|
# json.id team.fetch('id')
# json.name team.fetch('name')
# json.members team.fetch('members')
# json.role retrive_role_name(team.fetch('role') { nil })
# json.current_team team.fetch('current_team')
# json.can_be_leaved team.fetch('can_be_leaved')
json.users team_users do |team_user|
json.id team_user.user.id
json.email team_user.user.full_name
json.role team_user.role_str
json.created_at I18n.l(team_user.created_at, format: :full_date)
json.status team_user.user.active_status_str
json.actions do
json.current_role team_user.role_str
json.team_user_id team_user.id
end
end
end

View file

@ -1,5 +1,11 @@
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
config.after_initialize do
Bullet.enable = true
Bullet.bullet_logger = true
Bullet.raise = true # raise an error if n+1 query occurs
end
# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true

View file

@ -0,0 +1,62 @@
require 'rails_helper'
describe ClientApi::Teams::TeamsController, type: :controller do
login_user
before do
@user_one = User.first
@user_two = FactoryGirl.create(:user, email: 'sec_user@asdf.com')
@team_one = FactoryGirl.create :team
@team_two = FactoryGirl.create :team, name: 'Team two'
FactoryGirl.create :user_team, team: @team_one, user: @user_one, role: 2
end
describe 'GET #index' do
it 'should return HTTP success response' do
get :index, format: :json
expect(response).to be_success
expect(response).to have_http_status(:ok)
end
end
describe 'POST #change_team' do
it 'should return HTTP success response' do
FactoryGirl.create :user_team, team: @team_two, user: @user_one, role: 2
@user_one.update_attribute(:current_team_id, @team_one.id)
post :change_team, params: { team_id: @team_two.id }, as: :json
expect(response).to have_http_status(:ok)
end
it 'should return HTTP unprocessable_entity response if user not in team' do
@user_one.update_attribute(:current_team_id, @team_one.id)
post :change_team, params: { team_id: @team_two.id }, as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
it 'should return HTTP success response if same team as current' do
@user_one.update_attribute(:current_team_id, @team_one.id)
post :change_team, params: { team_id: @team_one.id }, as: :json
expect(response).to have_http_status(:ok)
end
end
describe 'GET #details' do
it 'should return HTTP success response' do
FactoryGirl.create :user_team, team: @team_two, user: @user_one, role: 2
@user_one.update_attribute(:current_team_id, @team_one.id)
get :details, params: { team_id: @team_two.id }, as: :json
expect(response).to have_http_status(:ok)
end
it 'should return HTTP unprocessable_entity response if user not in team' do
@user_one.update_attribute(:current_team_id, @team_one.id)
get :details, params: { team_id: @team_two.id }, as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
it 'should return HTTP unprocessable_entity response if team_id not valid' do
get :details, params: { team_id: 'banana' }, as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

View file

@ -10,20 +10,20 @@ describe ClientApi::Users::UserTeamsController, type: :controller do
FactoryGirl.create :user_team, team: @team, user: @user_one, role: 2
end
it 'Returns HTTP success if user can leave the team' do
it 'should return HTTP success if user can leave the team' do
FactoryGirl.create :user_team, team: @team, user: @user_two, role: 2
delete :leave_team, params: { team: @team.id }, format: :json
expect(response).to be_success
expect(response).to have_http_status(200)
expect(response).to have_http_status(:ok)
end
it 'Returns HTTP unprocessable_entity if user can\'t leave the team' do
it 'should return HTTP unprocessable_entity if user can\'t leave the team' do
delete :leave_team, params: { team: @team.id }, format: :json
expect(response).to_not be_success
expect(response).to have_http_status(:unprocessable_entity)
end
it 'Returns HTTP unprocessable_entity if no params given' do
it 'should return HTTP unprocessable_entity if no params given' do
delete :leave_team, format: :json
expect(response).to_not be_success
expect(response).to have_http_status(:unprocessable_entity)

View file

@ -0,0 +1,64 @@
require 'rails_helper'
describe ClientApi::TeamsService do
let(:team_one) { create :team }
let(:user_one) { create :user }
it 'should raise an ClientApi::CustomTeamError if user is not assigned' do
expect {
ClientApi::TeamsService.new(team_id: team_one.id)
}.to raise_error(ClientApi::CustomTeamError)
end
it 'should raise an ClientApi::CustomTeamError if team is not assigned' do
expect {
ClientApi::TeamsService.new(current_user: user_one)
}.to raise_error(ClientApi::CustomTeamError)
end
it 'should raise an ClientApi::CustomTeamError if team is not user team' do
expect {
ClientApi::TeamsService.new(current_user: user_one, team_id: team_one.id)
}.to raise_error(ClientApi::CustomTeamError)
end
describe '#change_current_team!' do
let(:team_two) { create :team, name: 'team two' }
let(:user_two) do
create :user, current_team_id: team_one.id, email: 'user_two@test.com'
end
it 'should change user current team' do
create :user_team, user: user_two, team: team_two
teams_service = ClientApi::TeamsService.new(current_user: user_two,
team_id: team_two.id)
teams_service.change_current_team!
expect(user_two.current_team_id).to eq team_two.id
end
end
describe '#team_page_details_data' do
let(:team_service) do
ClientApi::TeamsService.new(current_user: user_one, team_id: team_one.id)
end
it 'should return team page data' do
user_team = create :user_team, user: user_one, team: team_one
data = team_service.team_page_details_data
expect(data.fetch(:team).name).to eq team_one.name
expect(data.fetch(:team_users).first).to eq user_team
end
end
describe '#teams_data' do
let(:team_service) do
ClientApi::TeamsService.new(current_user: user_one, team_id: team_one.id)
end
it 'should return user teams' do
create :user_team, user: user_one, team: team_one
data = team_service.teams_data.fetch(:teams)
expect(data.first.fetch('name')).to eq team_one.name
end
end
end

View file

@ -16,6 +16,8 @@
require 'capybara/rspec'
require 'simplecov'
require 'faker'
require 'active_record'
require 'bullet'
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
@ -97,4 +99,15 @@ RSpec.configure do |config|
# as the one that triggered the failure.
Kernel.srand config.seed
=end
# Enable bullet gem in tests
if Bullet.enable?
config.before(:each) do
Bullet.start_request
end
config.after(:each) do
Bullet.perform_out_of_channel_notifications if Bullet.notification?
Bullet.end_request
end
end
end