Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
wattsworth
/
lumen-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
85170001
authored
Sep 20, 2019
by
source_reader
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
added coverage for services
parent
dc486e8d
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
66 additions
and
63 deletions
app/services/data_view/create_data_view.rb
app/services/db_stream/edit_stream.rb
app/services/nilm/create_nilm.rb
app/services/perimssion/create_permission.rb
app/services/service_status.rb
spec/models/data_app_spec.rb
spec/services/nilm/add_nilm_by_key_spec.rb
spec/services/nilm/add_nilm_by_user_spec.rb
spec/services/service_status_spec.rb
spec/services/users/invite_user_spec.rb
app/services/data_view/create_data_view.rb
View file @
85170001
...
...
@@ -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
...
...
app/services/db_stream/edit_stream.rb
View file @
85170001
...
...
@@ -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
app/services/nilm/create_nilm.rb
View file @
85170001
...
...
@@ -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
...
...
app/services/perimssion/create_permission.rb
deleted
100644 → 0
View file @
dc486e8d
# 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
app/services/service_status.rb
View file @
85170001
...
...
@@ -91,11 +91,4 @@ module ServiceStatus
true
end
def
as_json
(
_options
=
{})
{
notices:
@notices
,
errors:
@errors
,
warnings:
@warnings
}
end
end
spec/models/data_app_spec.rb
View file @
85170001
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
spec/services/nilm/add_nilm_by_key_spec.rb
View file @
85170001
...
...
@@ -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"
}
...
...
spec/services/nilm/add_nilm_by_user_spec.rb
View file @
85170001
...
...
@@ -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"
,
...
...
spec/services/service_status_spec.rb
View file @
85170001
...
...
@@ -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
}
...
...
spec/services/users/invite_user_spec.rb
View file @
85170001
...
...
@@ -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
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment