Commit 91a5fad1 by John Doe

upgraded to ruby 2.5, many changes to joule module api

parent 21bcff06
...@@ -61,7 +61,7 @@ group :development, :test do ...@@ -61,7 +61,7 @@ group :development, :test do
gem 'capistrano-passenger' gem 'capistrano-passenger'
end end
group :local, :development, :test do group :local, :development, :test do
gem 'factory_girl_rails' gem 'factory_bot_rails'
gem 'faker' gem 'faker'
# Use sqlite3 as the database for Active Record # Use sqlite3 as the database for Active Record
gem 'sqlite3' gem 'sqlite3'
......
...@@ -117,10 +117,10 @@ GEM ...@@ -117,10 +117,10 @@ GEM
docile (1.3.0) docile (1.3.0)
erubi (1.7.1) erubi (1.7.1)
execjs (2.7.0) execjs (2.7.0)
factory_girl (4.9.0) factory_bot (4.8.2)
activesupport (>= 3.0.0) activesupport (>= 3.0.0)
factory_girl_rails (4.9.0) factory_bot_rails (4.8.2)
factory_girl (~> 4.9.0) factory_bot (~> 4.8.2)
railties (>= 3.0.0) railties (>= 3.0.0)
faker (1.8.7) faker (1.8.7)
i18n (>= 0.7) i18n (>= 0.7)
...@@ -360,7 +360,7 @@ DEPENDENCIES ...@@ -360,7 +360,7 @@ DEPENDENCIES
database_cleaner database_cleaner
devise_invitable devise_invitable
devise_token_auth devise_token_auth
factory_girl_rails factory_bot_rails
faker faker
guard guard
guard-rspec guard-rspec
......
...@@ -195,6 +195,7 @@ class DbAdapter ...@@ -195,6 +195,7 @@ class DbAdapter
def __get_metadata(path) def __get_metadata(path)
dump = self.class.get("#{@url}/stream/get_metadata?path=#{path}") dump = self.class.get("#{@url}/stream/get_metadata?path=#{path}")
# find legacy parameters in raw metadata # find legacy parameters in raw metadata
#TODO: why is parsed_response a string sometimes?? <error>
metadata = dump.parsed_response.except('config_key__') metadata = dump.parsed_response.except('config_key__')
# parse values from config_key entry if it exists # parse values from config_key entry if it exists
config_key = JSON.parse(dump.parsed_response['config_key__'] || '{}') config_key = JSON.parse(dump.parsed_response['config_key__'] || '{}')
......
...@@ -3,26 +3,43 @@ class InterfacesController < ActionController::Base ...@@ -3,26 +3,43 @@ class InterfacesController < ActionController::Base
#GET /authenticate #GET /authenticate
def authenticate def authenticate
token = InterfaceAuthToken.find(params[:token])
#check if token timestamp is valid
puts("this session is: #{session[:test]}")
#sign_in(User.first)
reset_session reset_session
token = InterfaceAuthToken.find_by_id(params[:token])
render :unauthorized and return if token.nil?
render :unauthorized and return if token.expiration < Time.now
token.destroy
session[:user_id]=token.user.id session[:user_id]=token.user.id
session[:interface_id]=token.joule_module.id session[:interface_id]=token.joule_module.id
#interface_user_session(interface: token.interface.id) render plain: "welcome #{token.user.email}"
#redirect '/'
end end
#GET /logout #GET /logout
def logout def logout
interface_user_sign_out reset_session
redirect '/'
end end
#everything else is proxied #everything else is proxied
def get
render 'ok, you got it'
end
def put
end
def post
end
def delete
end
private private
def authenticate_interface_user! def authenticate_interface_user!
puts 'authenticating...' @current_user = User.find_by_id(session[:user_id])
render :unauthorized if @current_user.nil?
#verify the session matches the URL
#verify the user has permissions on this module
end end
end end
class JouleModulesController < ApplicationController
before_action :authenticate_user!
def show
@joule_module = JouleModule.find(params[:id])
@nilm = @joule_module.nilm
head :unauthorized and return unless current_user.views_nilm?(@nilm)
if(@joule_module.web_interface)
@auth_token = InterfaceAuthToken.create(joule_module: @joule_module,
user: current_user, expiration: 5.minutes.from_now)
end
render and return
end
end
...@@ -17,7 +17,6 @@ class NilmsController < ApplicationController ...@@ -17,7 +17,6 @@ class NilmsController < ApplicationController
def show def show
#render the database and joule modules #render the database and joule modules
@role = current_user.get_nilm_permission(@nilm) @role = current_user.get_nilm_permission(@nilm)
@url_template = "http://%s.modules.wattsworth.local"
#request new information from the NILM #request new information from the NILM
if(params[:refresh]) if(params[:refresh])
@service = UpdateNilm.new() @service = UpdateNilm.new()
......
class InterfaceAuthToken < ApplicationRecord class InterfaceAuthToken < ApplicationRecord
belongs_to :joule_module
belongs_to :user
after_initialize do |auth_token|
auth_token.value ||= SecureRandom.hex
end
def url
"http://localhost:3000/interfaces/#{joule_module.id}/authenticate?token=#{value}"
end
end end
<h1>You are not logged in.
Use the Data Explorer to access this interface.</h1>
<%= image_tag "robot_stop_707x707.jpg" %>
json.data do
json.array! @joule_modules do |m|
json.extract! m, *JouleModule.json_keys
json.url @url_template % [m.id]
json.nilm_id @nilm.id
end
end
json.partial! "helpers/messages", service: @service json.extract! @joule_module, *JouleModule.json_keys
json.nilm_id @nilm.id
if @joule_module.web_interface
json.url @auth_token.url
end
...@@ -8,7 +8,6 @@ json.data do ...@@ -8,7 +8,6 @@ json.data do
end end
json.jouleModules(@nilm.joule_modules) do |m| json.jouleModules(@nilm.joule_modules) do |m|
json.extract! m, *JouleModule.json_keys json.extract! m, *JouleModule.json_keys
json.url @url_template % [m.id]
json.nilm_id @nilm.id json.nilm_id @nilm.id
end end
end end
......
...@@ -5,6 +5,9 @@ Rails.application.routes.draw do ...@@ -5,6 +5,9 @@ Rails.application.routes.draw do
put 'refresh' put 'refresh'
end end
end end
resources :joule_modules, only: [:show]
resources :data_views do resources :data_views do
collection do collection do
get 'home' #retrieve a user's home data view get 'home' #retrieve a user's home data view
...@@ -46,4 +49,5 @@ Rails.application.routes.draw do ...@@ -46,4 +49,5 @@ Rails.application.routes.draw do
end end
get 'interfaces/authenticate', to: 'interfaces#authenticate' get 'interfaces/authenticate', to: 'interfaces#authenticate'
get 'interfaces/*path', to: 'interfaces#get'
end end
class CreateInterfaceAuthTokens < ActiveRecord::Migration[5.1] class CreateInterfaceAuthTokens < ActiveRecord::Migration[5.1]
def change def change
create_table :interface_auth_tokens do |t| create_table :interface_auth_tokens do |t|
t.belongs_to :user, index: true
t.belongs_to :joule_module, index: true
t.string :value
t.datetime :expiration
t.timestamps t.timestamps
end end
end end
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180224021655) do ActiveRecord::Schema.define(version: 20180505193715) do
create_table "data_views", force: :cascade do |t| create_table "data_views", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
...@@ -104,6 +104,17 @@ ActiveRecord::Schema.define(version: 20180224021655) do ...@@ -104,6 +104,17 @@ ActiveRecord::Schema.define(version: 20180224021655) do
t.boolean "available" t.boolean "available"
end end
create_table "interface_auth_tokens", force: :cascade do |t|
t.integer "user_id"
t.integer "joule_module_id"
t.string "value"
t.datetime "expiration"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["joule_module_id"], name: "index_interface_auth_tokens_on_joule_module_id"
t.index ["user_id"], name: "index_interface_auth_tokens_on_user_id"
end
create_table "joule_modules", force: :cascade do |t| create_table "joule_modules", force: :cascade do |t|
t.string "name" t.string "name"
t.string "description" t.string "description"
......
require 'factory_girl_rails' require 'factory_bot_rails'
ADMIN_NAME="admin" ADMIN_NAME="admin"
ADMIN_PASSWORD ="password" ADMIN_PASSWORD ="password"
...@@ -16,7 +16,7 @@ namespace :local do ...@@ -16,7 +16,7 @@ namespace :local do
if(@admin.nil?) if(@admin.nil?)
puts 'Creating new admin user' puts 'Creating new admin user'
@admin = FactoryGirl.create(:user, @admin = FactoryBot.create(:user,
first_name: 'John', first_name: 'John',
last_name: 'Doe', last_name: 'Doe',
password: ADMIN_PASSWORD, password: ADMIN_PASSWORD,
......
require 'rails_helper'
RSpec.describe JouleModulesController, type: :controller do
end
...@@ -131,8 +131,8 @@ RSpec.describe NilmsController, type: :request do ...@@ -131,8 +131,8 @@ RSpec.describe NilmsController, type: :request do
get "/nilms/#{john_nilm.id}.json", get "/nilms/#{john_nilm.id}.json",
headers: john.create_new_auth_token headers: john.create_new_auth_token
body = JSON.parse(response.body) body = JSON.parse(response.body)
expect(body['data']['joule_modules'][0]['name']).to eq(test_module.name) expect(body['data']['jouleModules'][0]['name']).to eq(test_module.name)
expect(body['data']['joule_modules'][0]['url']).to start_with("http://#{test_module.id}.modules") expect(body['data']['jouleModules'][0]['url']).to start_with("http://#{test_module.id}.modules")
end end
it 'refreshes nilm data when requested' do it 'refreshes nilm data when requested' do
@auth_headers = john.create_new_auth_token @auth_headers = john.create_new_auth_token
......
FactoryGirl.define do FactoryBot.define do
factory :data_view do factory :data_view do
name { Faker::Lorem.words(3).join(' ') } name { Faker::Lorem.words(3).join(' ') }
description { Faker::Lorem.sentence } description { Faker::Lorem.sentence }
......
# frozen_string_literal: true # frozen_string_literal: true
FactoryGirl.define do FactoryBot.define do
factory :db_decimation do factory :db_decimation do
end end
......
# frozen_string_literal: true # frozen_string_literal: true
# generic DbStream # generic DbStream
FactoryGirl.define do FactoryBot.define do
factory :db_element do factory :db_element do
name { Faker::Lorem.unique.words(3).join(' ') } name { Faker::Lorem.unique.words(3).join(' ') }
units 'volts' units 'volts'
......
# frozen_string_literal: true # frozen_string_literal: true
# generic DbFolder # generic DbFolder
FactoryGirl.define do FactoryBot.define do
factory :db_folder, aliases: [:root_folder] do factory :db_folder, aliases: [:root_folder] do
name { Faker::Lorem.word } name { Faker::Lorem.word }
description { Faker::Lorem.sentence } description { Faker::Lorem.sentence }
......
# frozen_string_literal: true # frozen_string_literal: true
# generic DbStream # generic DbStream
FactoryGirl.define do FactoryBot.define do
factory :db_stream do factory :db_stream do
name { Faker::Lorem.words(3).join(' ') } name { Faker::Lorem.words(3).join(' ') }
name_abbrev { Faker::Lorem.word } name_abbrev { Faker::Lorem.word }
......
# frozen_string_literal: true # frozen_string_literal: true
FactoryGirl.define do FactoryBot.define do
factory :db do factory :db do
url "" url ""
max_points_per_plot { Faker::Number.number(3) } max_points_per_plot { Faker::Number.number(3) }
......
FactoryGirl.define do FactoryBot.define do
factory :interface_auth_token do factory :interface_auth_token do
end end
......
FactoryGirl.define do FactoryBot.define do
factory :joule_module do factory :joule_module do
name { Faker::Lorem.words(3).join(' ') } name { Faker::Lorem.words(3).join(' ') }
description { Faker::Lorem.sentence } description { Faker::Lorem.sentence }
......
# frozen_string_literal: true # frozen_string_literal: true
FactoryGirl.define do FactoryBot.define do
......
FactoryGirl.define do FactoryBot.define do
factory :permission do factory :permission do
nilm nilm
role "admin" role "admin"
......
FactoryGirl.define do FactoryBot.define do
factory :user_group do factory :user_group do
transient do transient do
members [] members []
......
FactoryGirl.define do FactoryBot.define do
factory :user do factory :user do
first_name {Faker::Name.first_name} first_name {Faker::Name.first_name}
last_name {Faker::Name.first_name} last_name {Faker::Name.first_name}
......
...@@ -22,9 +22,9 @@ RSpec.describe 'DbElement' do ...@@ -22,9 +22,9 @@ RSpec.describe 'DbElement' do
expect(element.errors[:name].any?).to be true expect(element.errors[:name].any?).to be true
end end
it 'name is unique in stream' do it 'name is unique in stream' do
stream = FactoryGirl.create(:db_stream, name: 'parent') stream = FactoryBot.create(:db_stream, name: 'parent')
elem1 = FactoryGirl.create(:db_element, name: 'shared', db_stream: stream) elem1 = FactoryBot.create(:db_element, name: 'shared', db_stream: stream)
elem2 = FactoryGirl.build(:db_element, name: 'shared', db_stream: stream) elem2 = FactoryBot.build(:db_element, name: 'shared', db_stream: stream)
elem2.validate elem2.validate
expect(elem2.errors[:name].any?).to be true expect(elem2.errors[:name].any?).to be true
# but if element is in a different stream its ok # but if element is in a different stream its ok
......
...@@ -36,8 +36,8 @@ RSpec.describe 'DbFolder' do ...@@ -36,8 +36,8 @@ RSpec.describe 'DbFolder' do
end end
describe 'insert_stream' do describe 'insert_stream' do
let(:db_folder) { FactoryGirl.create(:db_folder) } let(:db_folder) { FactoryBot.create(:db_folder) }
let(:new_stream) { FactoryGirl.create(:db_stream) } let(:new_stream) { FactoryBot.create(:db_stream) }
it 'adds the stream to the folder' do it 'adds the stream to the folder' do
db_folder.insert_stream(stream: new_stream) db_folder.insert_stream(stream: new_stream)
...@@ -46,7 +46,7 @@ RSpec.describe 'DbFolder' do ...@@ -46,7 +46,7 @@ RSpec.describe 'DbFolder' do
end end
describe 'validation' do describe 'validation' do
let(:db_folder) { FactoryGirl.create(:db_folder) } let(:db_folder) { FactoryBot.create(:db_folder) }
it 'forbids an empty name' do it 'forbids an empty name' do
db_folder.name = '' db_folder.name = ''
expect(db_folder.valid?).to be false expect(db_folder.valid?).to be false
......
...@@ -6,7 +6,7 @@ require File.expand_path('../../config/environment', __FILE__) ...@@ -6,7 +6,7 @@ require File.expand_path('../../config/environment', __FILE__)
abort('The Rails environment is running in production mode!') if Rails.env.production? abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'spec_helper' require 'spec_helper'
require 'rspec/rails' require 'rspec/rails'
require 'support/factory_girl' require 'support/factory_bot'
require 'support/api_messages' require 'support/api_messages'
require 'support/mock_service' require 'support/mock_service'
......
...@@ -4,7 +4,7 @@ require 'rails_helper' ...@@ -4,7 +4,7 @@ require 'rails_helper'
describe 'EditStream service' do describe 'EditStream service' do
let(:db_adapter) { instance_double(DbAdapter) } let(:db_adapter) { instance_double(DbAdapter) }
let(:stream) { FactoryGirl.create(:db_stream, path: '/stream/path', name: 'old') } let(:stream) { FactoryBot.create(:db_stream, path: '/stream/path', name: 'old') }
let(:element) { stream.db_elements.first} let(:element) { stream.db_elements.first}
let(:service) { EditStream.new(db_adapter) } let(:service) { EditStream.new(db_adapter) }
# db adapter return values # db adapter return values
......
# frozen_string_literal: true # frozen_string_literal: true
RSpec.configure do |config| RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods config.include FactoryBot::Syntax::Methods
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