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
gem 'capistrano-passenger'
end
group :local, :development, :test do
gem 'factory_girl_rails'
gem 'factory_bot_rails'
gem 'faker'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
......
......@@ -117,10 +117,10 @@ GEM
docile (1.3.0)
erubi (1.7.1)
execjs (2.7.0)
factory_girl (4.9.0)
factory_bot (4.8.2)
activesupport (>= 3.0.0)
factory_girl_rails (4.9.0)
factory_girl (~> 4.9.0)
factory_bot_rails (4.8.2)
factory_bot (~> 4.8.2)
railties (>= 3.0.0)
faker (1.8.7)
i18n (>= 0.7)
......@@ -360,7 +360,7 @@ DEPENDENCIES
database_cleaner
devise_invitable
devise_token_auth
factory_girl_rails
factory_bot_rails
faker
guard
guard-rspec
......
......@@ -195,6 +195,7 @@ class DbAdapter
def __get_metadata(path)
dump = self.class.get("#{@url}/stream/get_metadata?path=#{path}")
# find legacy parameters in raw metadata
#TODO: why is parsed_response a string sometimes?? <error>
metadata = dump.parsed_response.except('config_key__')
# parse values from config_key entry if it exists
config_key = JSON.parse(dump.parsed_response['config_key__'] || '{}')
......
......@@ -3,26 +3,43 @@ class InterfacesController < ActionController::Base
#GET /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
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[:interface_id]=token.joule_module.id
#interface_user_session(interface: token.interface.id)
#redirect '/'
render plain: "welcome #{token.user.email}"
end
#GET /logout
def logout
interface_user_sign_out
reset_session
redirect '/'
end
#everything else is proxied
def get
render 'ok, you got it'
end
def put
end
def post
end
def delete
end
private
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
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
def show
#render the database and joule modules
@role = current_user.get_nilm_permission(@nilm)
@url_template = "http://%s.modules.wattsworth.local"
#request new information from the NILM
if(params[:refresh])
@service = UpdateNilm.new()
......
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
<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
end
json.jouleModules(@nilm.joule_modules) do |m|
json.extract! m, *JouleModule.json_keys
json.url @url_template % [m.id]
json.nilm_id @nilm.id
end
end
......
......@@ -5,6 +5,9 @@ Rails.application.routes.draw do
put 'refresh'
end
end
resources :joule_modules, only: [:show]
resources :data_views do
collection do
get 'home' #retrieve a user's home data view
......@@ -46,4 +49,5 @@ Rails.application.routes.draw do
end
get 'interfaces/authenticate', to: 'interfaces#authenticate'
get 'interfaces/*path', to: 'interfaces#get'
end
class CreateInterfaceAuthTokens < ActiveRecord::Migration[5.1]
def change
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
end
end
......
......@@ -10,7 +10,7 @@
#
# 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|
t.integer "user_id"
......@@ -104,6 +104,17 @@ ActiveRecord::Schema.define(version: 20180224021655) do
t.boolean "available"
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|
t.string "name"
t.string "description"
......
require 'factory_girl_rails'
require 'factory_bot_rails'
ADMIN_NAME="admin"
ADMIN_PASSWORD ="password"
......@@ -16,7 +16,7 @@ namespace :local do
if(@admin.nil?)
puts 'Creating new admin user'
@admin = FactoryGirl.create(:user,
@admin = FactoryBot.create(:user,
first_name: 'John',
last_name: 'Doe',
password: ADMIN_PASSWORD,
......
require 'rails_helper'
RSpec.describe JouleModulesController, type: :controller do
end
......@@ -131,8 +131,8 @@ RSpec.describe NilmsController, type: :request do
get "/nilms/#{john_nilm.id}.json",
headers: john.create_new_auth_token
body = JSON.parse(response.body)
expect(body['data']['joule_modules'][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]['name']).to eq(test_module.name)
expect(body['data']['jouleModules'][0]['url']).to start_with("http://#{test_module.id}.modules")
end
it 'refreshes nilm data when requested' do
@auth_headers = john.create_new_auth_token
......
FactoryGirl.define do
FactoryBot.define do
factory :data_view do
name { Faker::Lorem.words(3).join(' ') }
description { Faker::Lorem.sentence }
......
# frozen_string_literal: true
FactoryGirl.define do
FactoryBot.define do
factory :db_decimation do
end
......
# frozen_string_literal: true
# generic DbStream
FactoryGirl.define do
FactoryBot.define do
factory :db_element do
name { Faker::Lorem.unique.words(3).join(' ') }
units 'volts'
......
# frozen_string_literal: true
# generic DbFolder
FactoryGirl.define do
FactoryBot.define do
factory :db_folder, aliases: [:root_folder] do
name { Faker::Lorem.word }
description { Faker::Lorem.sentence }
......
# frozen_string_literal: true
# generic DbStream
FactoryGirl.define do
FactoryBot.define do
factory :db_stream do
name { Faker::Lorem.words(3).join(' ') }
name_abbrev { Faker::Lorem.word }
......
# frozen_string_literal: true
FactoryGirl.define do
FactoryBot.define do
factory :db do
url ""
max_points_per_plot { Faker::Number.number(3) }
......
FactoryGirl.define do
FactoryBot.define do
factory :interface_auth_token do
end
......
FactoryGirl.define do
FactoryBot.define do
factory :joule_module do
name { Faker::Lorem.words(3).join(' ') }
description { Faker::Lorem.sentence }
......
# frozen_string_literal: true
FactoryGirl.define do
FactoryBot.define do
......
FactoryGirl.define do
FactoryBot.define do
factory :permission do
nilm
role "admin"
......
FactoryGirl.define do
FactoryBot.define do
factory :user_group do
transient do
members []
......
FactoryGirl.define do
FactoryBot.define do
factory :user do
first_name {Faker::Name.first_name}
last_name {Faker::Name.first_name}
......
......@@ -22,9 +22,9 @@ RSpec.describe 'DbElement' do
expect(element.errors[:name].any?).to be true
end
it 'name is unique in stream' do
stream = FactoryGirl.create(:db_stream, name: 'parent')
elem1 = FactoryGirl.create(:db_element, name: 'shared', db_stream: stream)
elem2 = FactoryGirl.build(:db_element, name: 'shared', db_stream: stream)
stream = FactoryBot.create(:db_stream, name: 'parent')
elem1 = FactoryBot.create(:db_element, name: 'shared', db_stream: stream)
elem2 = FactoryBot.build(:db_element, name: 'shared', db_stream: stream)
elem2.validate
expect(elem2.errors[:name].any?).to be true
# but if element is in a different stream its ok
......
......@@ -36,8 +36,8 @@ RSpec.describe 'DbFolder' do
end
describe 'insert_stream' do
let(:db_folder) { FactoryGirl.create(:db_folder) }
let(:new_stream) { FactoryGirl.create(:db_stream) }
let(:db_folder) { FactoryBot.create(:db_folder) }
let(:new_stream) { FactoryBot.create(:db_stream) }
it 'adds the stream to the folder' do
db_folder.insert_stream(stream: new_stream)
......@@ -46,7 +46,7 @@ RSpec.describe 'DbFolder' do
end
describe 'validation' do
let(:db_folder) { FactoryGirl.create(:db_folder) }
let(:db_folder) { FactoryBot.create(:db_folder) }
it 'forbids an empty name' do
db_folder.name = ''
expect(db_folder.valid?).to be false
......
......@@ -6,7 +6,7 @@ require File.expand_path('../../config/environment', __FILE__)
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'support/factory_girl'
require 'support/factory_bot'
require 'support/api_messages'
require 'support/mock_service'
......
......@@ -4,7 +4,7 @@ require 'rails_helper'
describe 'EditStream service' do
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(:service) { EditStream.new(db_adapter) }
# db adapter return values
......
# frozen_string_literal: true
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include FactoryBot::Syntax::Methods
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