Commit a83de152 by source_reader

persistent app ids

parent 289c7283
......@@ -15,12 +15,18 @@ module Joule
return self
end
#remove the previous modules
@nilm.data_apps.destroy_all
#keep track of apps that already loaded
old_app_ids = @nilm.data_apps.map{|app| app.id}
app_schemas.each do |schema|
@nilm.data_apps << DataApp.new(name: schema[:name],
joule_id: schema[:id])
cur_app = @nilm.data_apps.where(name: schema[:name], joule_id: schema[:id]).first
if cur_app.nil?
@nilm.data_apps << DataApp.new(name: schema[:name],
joule_id: schema[:id])
else
old_app_ids.delete(cur_app.id)
end
end
DataApp.destroy(old_app_ids)
set_notice("refreshed apps")
self
......
......@@ -6,12 +6,13 @@ class DataAppController < ApplicationController
@app = DataApp.find(params[:id])
@nilm = @app.nilm
head :unauthorized and return unless current_user.views_nilm?(@nilm)
# destroy any existing tokens
InterfaceAuthToken.where(user: current_user, data_app: @app).destroy_all
@auth_url = _app_auth_url
if @auth_url.nil?
head :not_availble and return
head :not_implemented and return
end
end
......
[
{"id": "m1", "name": "Data App"},
{"id": "p0", "name": "jupyter"},
{"id": "p1", "name": "dashboard"}
]
\ No newline at end of file
# frozen_string_literal: true
require 'rails_helper'
describe Joule::UpdateApps do
it 'updates apps and maintains common id values' do
nilm = FactoryBot.create(:nilm, name: 'test nilm')
updater = Joule::UpdateApps.new(nilm)
raw = File.read(File.dirname(__FILE__)+"/apps.json")
json = JSON.parse(raw)
json.each do |item|
item.symbolize_keys!
end
updater.run(json)
app_ids = nilm.data_apps.map {|app| app.id}
# parses all three apps
expect(app_ids.length).to eq 3
# running with the same app json doesn't change the ID values
updater.run(json)
new_app_ids = nilm.data_apps.map {|app| app.id}
expect(new_app_ids).to eq app_ids
# removes unused apps
nilm.data_apps << DataApp.new(name: 'unused app', joule_id: 'm5')
expect(nilm.data_apps.length).to eq 4
updater.run(json)
nilm.data_apps.reload
new_app_ids = nilm.data_apps.map {|app| app.id}
expect(new_app_ids).to eq app_ids
end
end
\ No newline at end of file
......@@ -10,29 +10,36 @@ RSpec.describe DataAppController, type: :request do
describe 'GET show' do
context 'with any permissions' do
it 'returns the data_app as json' do
it 'returns unavaliable if not proxied' do
@auth_headers = owner.create_new_auth_token
get "/app/#{test_app.id}.json",
headers: @auth_headers
expect(response.status).to eq(501)
end
it 'returns a subdomain url if proxied with subdomain apps' do
@auth_headers = owner.create_new_auth_token
get "/app/#{test_app.id}.json",
headers: @auth_headers.merge({HTTP_X_SUBDOMAIN_APPS:'true',
HTTP_X_APP_SERVER_NAME:'test.net',
HTTP_X_APP_SERVER_SCHEME: 'https'})
expect(response.status).to eq(200)
expect(response.header['Content-Type']).to include('application/json')
body = JSON.parse(response.body)
expect(body["name"]).to eq test_app.name
# no URL if not proxied
expect(body["url"]).to eq '#'
expect(InterfaceAuthToken.count).to eq 0
token = InterfaceAuthToken.where(data_app:test_app).first
expect(body["url"]).to eq "https://#{test_app.id}.app.test.net?auth_token=#{token.value}"
end
it 'returns url if proxied' do
it 'returns same domain url if proxied without subdomain apps' do
@auth_headers = owner.create_new_auth_token
get "/app/#{test_app.id}.json",
headers: @auth_headers.merge({HTTP_X_APP_BASE_URI:'/lumen'})
headers: @auth_headers.merge({HTTP_X_SUBDOMAIN_APPS:'false', HTTP_X_APP_BASE_URI:'/lumen'})
expect(response.status).to eq(200)
expect(response.header['Content-Type']).to include('application/json')
body = JSON.parse(response.body)
expect(body["name"]).to eq test_app.name
# URL has an auth token
token = InterfaceAuthToken.where(data_app:test_app).first
expect(body["url"]).to end_with token.value
expect(body["url"]).to eq "/lumen/#{test_app.id}/?auth_token=#{token.value}"
end
end
context 'without permissions' do
......
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
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