Fix blinky user spec

pull/260/head
Rick Carlino 2016-09-02 10:15:54 -05:00
parent b0788fca36
commit a76cb932e7
12 changed files with 15 additions and 53 deletions

View File

@ -26,25 +26,19 @@ private
def null_device
@null_device ||= NullDevice.new(name: 'null_device',
uuid: '-',
token: '-')
uuid: '-')
end
def authenticate_user!
# All possible information that could be needed for any of the 3 auth
# strategies.
context = {
# bot_token: request.headers["HTTP_BOT_TOKEN"],
# bot_uuid: request.headers["HTTP_BOT_UUID"],
jwt: request.headers["Authorization"],
user: current_user }
# Returns a symbol representing the appropriate auth strategy, or nil if
# unknown.
strategy = Auth::DetermineAuthStrategy.run!(context)
case strategy
# when :bot
# # When a bot uses the API, there is no current_user.
# @current_device = Auth::Create.run!(context)
when :jwt
sign_in(Auth::FromJWT.run!(context))
when :already_connected

View File

@ -32,9 +32,7 @@ module Api
# Only allow a trusted parameter "white list" through.
def device_params
{ name: params[:name] || Haikunator.haikunate(99),
uuid: params[:uuid] || SecureRandom.uuid,
token: params[:token] || SecureRandom.hex }
{ name: params[:name], uuid: params[:uuid] }
end
end
end

View File

@ -9,17 +9,6 @@ class Device < ActiveRecord::Base
has_many :plants, dependent: :destroy
has_one :planting_area
# # The UUID of the device
# field :uuid
# validates :uuid, presence: true
# # The Authentication token for the device.
# # DEPRECATED LEGACY USE ONLY!
# field :token
# validates :token, presence: true
# # The 'Friendly Name' of the device. I recommend 'The Cabbage Patch Kid'
# field :name
# validates :name, presence: true
def if_not_null
yield(self)
self

View File

@ -1,16 +0,0 @@
require 'mutations/time_filter'
module Auth
class Create < Mutations::Command
required do
string :bot_token
string :bot_uuid
end
def execute
Device.find_by(uuid: bot_uuid, token: bot_token)
rescue ActiveRecord::RecordNotFound
add_error :auth, :*, 'Bad uuid or token'
end
end
end

View File

@ -8,7 +8,6 @@ module Devices
optional do
string :uuid
string :token
string :name
end
@ -30,7 +29,6 @@ module Devices
def merge_default_values
inputs[:uuid] ||= SecureRandom.uuid
inputs[:token] ||= SecureRandom.hex
inputs[:name] ||= Haikunator.haikunate(9999)
end

View File

@ -1,3 +1,3 @@
class DeviceSerializer < ActiveModel::Serializer
attributes :id, :name, :uuid, :token
attributes :id, :name, :uuid
end

View File

@ -29,7 +29,6 @@ class CreateEverything < ActiveRecord::Migration
create_table :devices do |t|
t.integer :planting_area_id
t.string :uuid
t.string :token
t.string :name
end

View File

@ -16,7 +16,6 @@ ActiveRecord::Schema.define(version: 20160820050202) do
create_table "devices", force: :cascade do |t|
t.integer "planting_area_id"
t.string "uuid"
t.string "token"
t.string "name"
end

View File

@ -11,7 +11,7 @@ describe Api::DevicesController do
it 'creates a new device for a user' do
sign_in user
params = {user_id: user.id, name: 'Frank', uuid: '123', token: '321'}
params = {user_id: user.id, name: 'Frank', uuid: '123'}
post :create, params
resp = JSON.parse(response.body)
new_device = Device.find(resp['id'])
@ -23,7 +23,7 @@ describe Api::DevicesController do
it 'shares devices between two users' do
bot = user.device
sign_in user2
params = {name: 'QQQ', uuid: bot.uuid, token: bot.token}
params = {name: 'QQQ', uuid: bot.uuid}
post :create, params
expect(user.reload.device.reload.name).to eq("QQQ")
expect(user2.reload.device.reload.name).to eq("QQQ")

View File

@ -14,7 +14,7 @@ describe Api::DevicesController do
it 'updates a Device' do
sign_in user
fake_name = Faker::Name.name
put :update, {id: user.device.id, name: fake_name, uuid: 1, token: 2}, format: :json
put :update, {id: user.device.id, name: fake_name, uuid: 1}, format: :json
# put path, params, options
user.reload
device = user.reload.device.reload
@ -26,18 +26,17 @@ describe Api::DevicesController do
user.update_attributes(device: nil)
sign_in user
fake_name = Faker::Name.name
put :update, {uuid: 1, token: 2}, format: :json
put :update, {uuid: 1}, format: :json
user.reload
expect(user.device).to be_kind_of(Device)
expect(user.device['name'].length).to be > 4 # Haikunator
expect(user.device['uuid']).to eq('1')
expect(user.device['token']).to eq('2')
end
it 'shares devices between two users' do
bot = user.device
sign_in user2
post :update, { name: 'Frank', uuid: bot.uuid, token: bot.token }
post :update, { name: 'Frank', uuid: bot.uuid }
user.reload
user2.reload
expect(user.device.id).to eq(user2.device.id)

View File

@ -7,25 +7,28 @@ describe Api::UsersController do
describe '#create' do
it 'creates a new user' do
email = Faker::Internet.email
original_count = User.count
params = { password_confirmation: "Password123",
password: "Password123",
email: "t@g.com",
email: email,
name: "Frank" }
post :create, params
expect(User.count > original_count).to be_truthy
expect(User.count).to eq(original_count + 1)
user = User.find json[:user][:id]
expect(user.name).to eq("Frank")
expect(user.email).to eq("t@g.com")
expect(user.email).to eq(email)
expect(user.valid_password?('Password123')).to be_truthy
expect(json[:token][:unencoded][:sub]).to eq(user.email)
end
it 'handles password confirmation mismatch' do
email = Faker::Internet.email
original_count = User.count
params = { password_confirmation: "Password123",
password: "Password321",
email: "t@g.com",
email: email,
name: "Frank" }
post :create, params
expect(User.count > original_count).to be_falsy

View File

@ -5,6 +5,5 @@ FactoryGirl.define do
factory :device do
name { Faker::Internet.user_name }
uuid { SecureRandom.uuid }
token { SecureRandom.urlsafe_base64 }
end
end