require "rails_helper" #-#-#-#-#REST#-#-#-#-# RSpec.describe ::People::V1::User, :type => :request do describe "Index" do before(:example) do @user = FactoryGirl.create(:people_user_1) token = @user.tokens[0].auth_token @header = {"Auth-Token" => token, "Email" => @user.email} end # get /api/1/users it "checks index json response" do FactoryGirl.create_list(:people_user_1, 10) get 'api/1/users', nil, @header expect(response.status).to eq(200) #ok expect(json["users"]).to_not eq(nil) end end end RSpec.describe ::People::V1::User, :type => :request do describe "Show" do before(:example) do @user = FactoryGirl.create(:people_user_1) token = @user.tokens[0].auth_token @header = {"Auth-Token" => token, "Email" => @user.email} end # get /api/1/users/1 it "checks show json response" do get 'api/1/users/1', nil, @header expect(response.status).to eq(200) #ok expect(json["user"]["id"]).to eq(1) end end end RSpec.describe ::People::V1::User, :type => :request do describe "Update" do before(:example) do @user = FactoryGirl.create(:people_user_1) token = @user.tokens[0].auth_token @header = {"Auth-Token" => token, "Email" => @user.email} end # put /api/1/users/1 it "checks update json response" do attrs = FactoryGirl.attributes_for(:people_user_1) attrs["email"] = "Cole@LaunchU.com" hash = {"user" => attrs} #This uses users 1 header put 'api/1/users/1', hash, @header expect(response.status).to eq(200) #ok expect(json["user"]["email"]).to eq("Cole@LaunchU.com") end it "makes sure a user cannot alter another users information" do FactoryGirl.create(:people_user_1) attrs = FactoryGirl.attributes_for(:people_user_1) attrs["email"] = "Cole@LaunchU.com" hash = {"user" => attrs} #This uses users 2 header put 'api/1/users/2', hash, @header expect(response.status).to eq(403) #forbidden end it "makes sure a user can't update to bad values" do attrs = FactoryGirl.attributes_for(:people_user_1) attrs["email"] = "" hash = {"user" => attrs} #This uses users 1 header put 'api/1/users/1', hash, @header expect(response.status).to eq(422) #ok end end end #-#-#-#-#Collection Routes#-#-#-#-# RSpec.describe ::People::V1::User, :type => :request do describe "Register" do before(:example) do @attrs = FactoryGirl.attributes_for(:people_user_1) end # post /api/1/users/register it "checks response of a register request with a valid user object" do post 'api/1/users/register', :user => @attrs expect(response.status).to eq(200) #ok expect(::People::V1::User.all.count).to eq(1) #A valid token needs to be returned expect(json["token"]["auth_token"]).to_not eq(nil) token = ::People::V1::User.find(1).tokens[0] expect(json["token"]["auth_token"]).to eq(token.auth_token) end end end RSpec.describe ::People::V1::User, :type => :request do describe "Login" do before(:example) do @attrs = FactoryGirl.attributes_for(:people_user_1) end # post /api/1/users/login it "checks response of a register request with mismatched passwords" do @attrs["password"] = "password1" @attrs["password_confirmation"] = "password2" post 'api/1/users/register', :user => @attrs expect(response.status).to eq(422) #invalid_resource expect(::People::V1::User.all.count).to eq(0) #Errors need to be returned expect(json["errors"]).to_not eq(nil) end # post /api/1/users/login it "checks response of a valid login request" do @attrs["password"] = "password123" @attrs["password_confirmation"] = "password123" user = FactoryGirl.create(:people_user_1,@attrs) old_auth_token = user.tokens[0].auth_token #It needs to send an email and password post 'api/1/users/login', @attrs expect(response.status).to eq(200) #ok #A valid and new token need to be returned expect(json["token"]["auth_token"]).to_not eq(old_auth_token) #This token should be the newest token in the database new_auth_token = ::People::V1::User.find(1).tokens.order("created_at").last.auth_token expect(json["token"]["auth_token"]).to eq(new_auth_token) end # post /api/1/users/login it "checks response of an invalid login request with an invalid password" do @attrs["password"] = "password123" @attrs["password_confirmation"] = "password123" user = FactoryGirl.create(:people_user_1,@attrs) #The password needs to be invalid @attrs["password"] = "wrongPassword" #It needs to send an email and password post 'api/1/users/login', @attrs expect(response.status).to eq(401) #unauthorized #Errors need to be returned expect(json["errors"]).to_not eq(nil) end end end RSpec.describe ::People::V1::User, :type => :request do describe "Logout" do before(:example) do @attrs = FactoryGirl.attributes_for(:people_user_1) end # get /api/1/users/1/logout it "checks logout response and makes sure token is deleted" do user = FactoryGirl.create(:people_user_1,@attrs) token = user.tokens[0].auth_token header = {"Auth-Token" => token, "Email" => user.email} get 'api/1/users/logout', nil, header expect(::People::V1::User.count).to eq(1) expect(response.status).to eq(200) #ok expect(::Arcadex::Token.count).to eq(0) end end end #-#-#-#-#Serialization#-#-#-#-# RSpec.describe ::People::V1::User, :type => :request do describe "Serialization" do before(:example) do @user = FactoryGirl.create(:people_user_1) token = @user.tokens[0].auth_token @header = {"Auth-Token" => token, "Email" => @user.email} end # get /api/1/users it "checks only appropriate attributes are sent back for index" do FactoryGirl.create_list(:people_user_1, 10) get 'api/1/users', nil, @header expect(response.status).to eq(200) #ok expect(::People::V1::User.count).to eq(11) expect(::Arcadex::Token.count).to eq(11) expect(json["users"][0]["password_digest"]).to eq(nil) expect(json["users"][0]["created_at"]).to_not eq(nil) expect(json["users"][0]["updated_at"]).to_not eq(nil) end end end #-#-#-#-#Errors#-#-#-#-# RSpec.describe ::People::V1::User, :type => :request do describe "Authorization" do before(:example) do @user = FactoryGirl.create(:people_user_1) token = @user.tokens[0].auth_token @header = {"Auth-Token" => token, "Email" => @user.email} end it "checks for 404 response" do get 'api/1/users/20', nil, @header expect(response.status).to eq(404) #not_found end end end