Commit 85170001 by source_reader

added coverage for services

parent dc486e8d
......@@ -13,6 +13,7 @@ class CreateDataView
@data_view = DataView.new(data_view_params.merge({owner: user}))
#resize thumbnail because client can upload any dimension
# :nocov:
if(!@data_view.image.nil? && !@data_view.image.empty?)
metadata = "data:image/png;base64,"
base64_string = @data_view.image[metadata.size..-1]
......@@ -23,6 +24,7 @@ class CreateDataView
scaled_image_bytes = image.to_blob
@data_view.image = metadata+Base64.strict_encode64(scaled_image_bytes)
end
#:nocov:
# build nilm associations for permissions
begin
......
......@@ -34,15 +34,4 @@ class EditStream
set_notice("Stream updated")
self
end
def __parse_element_attribs(attribs)
if !attribs.nil? && attribs.length>=1
attribs.map { |element|
{id: element[:id],
name: element[:name]}
}
else
[]
end
end
end
......@@ -26,13 +26,9 @@ class CreateNilm
# pass NILM url onto database since we are using
# a single endpoint (eventually this will be joule)
db = Db.new(nilm: @nilm, url: url)
unless db.valid?
add_errors(db.errors.full_messages.map{|msg| "Database: #{msg}"})
return self
end
#everything is valid, save the objects
nilm.save
db.save
nilm.save!
db.save!
#give the owner 'admin' permissions on the nilm
Permission.create(user: owner, nilm: nilm, role: 'admin')
#update the database
......
# frozen_string_literal: true
# Handles changing DbStream attributes
class CreatePermission
include ServiceStatus
def run(nilm, role, type, target_id)
# create [role] perimssion on [nilm] for
# the user or group specified
# [type]: user|group
# [target_id]: user_id or user_group_id value
#
@permission = Permission.create(nilm: nilm, role: role)
case type
when 'user'
if nilm.permissions.find_by_user_id(target_id)
add_error('user already has permissions on this nilm')
return self
end
@permission.user = User.find(target_id)
when 'group'
if nilm.permissions.find_by_user_groupe_id(target_id)
add_error('group already has permissions on this nilm')
return self
end
@permission.user_group = UserGroup.find(target_id)
else
add_error('invalid target_id')
return self
end
unless @permission.save
add_error(permission.errors.full_messages)
return self
end
set_notice('Created permission')
self
end
end
......@@ -91,11 +91,4 @@ module ServiceStatus
true
end
def as_json(_options = {})
{
notices: @notices,
errors: @errors,
warnings: @warnings
}
end
end
require 'rails_helper'
RSpec.describe DataApp, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
let(:data_app) { DataApp.new }
specify { expect(data_app).to respond_to(:name) }
specify { expect(data_app).to respond_to(:joule_id) }
specify { expect(data_app).to respond_to(:nilm) }
it 'creates app url' do
nilm = create(:nilm, url:"http://nilm/joule")
app = create(:data_app, joule_id: 4, nilm: nilm)
expect(app.url).to eq "http://nilm/joule/app/4/"
end
end
......@@ -29,6 +29,26 @@ RSpec.describe 'AddNilmByKey' do
expect(NilmAuthKey.count).to eq 0
end
it 'uses name in URL when requested' do
owner = create(:user)
key = NilmAuthKey.new(key: "random_key", user: owner)
key.save!
user_params = {auth_key: "random_key"}
nilm_params = {name: "secure.node", api_key: "api_key", port: 8088,
scheme: "https", base_uri: "/joule", name_is_host: true,
}
request_params = ActionController::Parameters.new(user_params.merge(nilm_params))
service = AddNilmByKey.new
service.run(request_params,"127.0.0.1")
expect(service.success?).to be true
# creates the nilm
nilm = service.nilm
expect(nilm.url).to eq "https://secure.node:8088/joule"
expect(nilm.name).to eq "secure.node"
expect(nilm.key).to eq "api_key"
end
it 'requires valid auth key' do
service = AddNilmByKey.new
user_params = {auth_key: "invalid"}
......
......@@ -24,6 +24,22 @@ RSpec.describe 'AddNilmByUser' do
# forwards warnings (can't contact made up installation)
expect(service.warnings?).to be true
end
it 'uses name in URL when requested' do
service = AddNilmByUser.new
user_params = {email: "bob@email.com", password: "password",
first_name: "Bob", last_name: "Test"}
nilm_params = {name: "secure.node", api_key: "api_key", port: 8088,
scheme: "https", base_uri: "/joule", name_is_host: true}
request_params = ActionController::Parameters.new(user_params.merge(nilm_params))
service.run(request_params,"127.0.0.1")
expect(service.success?).to be true
# creates the nilm
nilm = service.nilm
expect(nilm.url).to eq "https://secure.node:8088/joule"
expect(nilm.name).to eq "secure.node"
expect(nilm.key).to eq "api_key"
end
it 'requires all parameters' do
service = AddNilmByUser.new
user_params = {password: "missing_email",
......
......@@ -39,6 +39,17 @@ describe 'ServiceStatus' do
expect { x.run }.to raise_error(RuntimeError)
end
it 'resets messages' do
x = ModuleTester.new
x.add_error('error')
x.add_warning('warning')
x.add_notice('notice')
x.reset_messages
expect(x.errors).to eq []
expect(x.warnings).to eq []
expect(x.notices).to eq []
end
describe 'absorb_status' do
let(:parent) { ModuleTester.new }
let(:child) { ModuleTester.new }
......
......@@ -14,6 +14,11 @@ describe 'InviteUser service' do
expect(@invitee).to be_invited_to_sign_up
expect(@invitee.invited_by).to eq inviter
end
it 'returns error on invalid parameters' do
service.run(inviter,"","http://redirect.url")
expect(service.errors?).to be true
end
it 'sends the user an invitation' do
invitation = ActionMailer::Base.deliveries.last
expect(invitation.to).to eq [@invitee.email]
......
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