Commit ff791f4b by John Doe

adjusted json rendering for user groups

parent 8779108b
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# Wrapper around NilmDB HTTP service # Wrapper around NilmDB HTTP service
class DbAdapter class DbAdapter
include HTTParty include HTTParty
default_timeout 10
def initialize(url) def initialize(url)
@url = url @url = url
......
...@@ -5,7 +5,10 @@ class UserGroupsController < ApplicationController ...@@ -5,7 +5,10 @@ class UserGroupsController < ApplicationController
# GET /user_groups.json # GET /user_groups.json
def index def index
@user_groups = UserGroup.all @owned_groups = UserGroup.where(owner: current_user)
@member_groups = current_user.user_groups
my_groups = @member_groups+@owned_groups
@other_groups = UserGroup.where.not(id: my_groups.pluck(:id))
end end
# POST /user_groups.json # POST /user_groups.json
......
json.array! @user_groups do |group| json.owner do
json.array! @owned_groups do |group|
json.extract! group, *UserGroup.json_keys json.extract! group, *UserGroup.json_keys
json.members group.users do |user|
json.extract! user, *User.json_keys
end
end
end
json.member do
json.array! @member_groups do |group|
json.extract! group, *UserGroup.json_keys
end
end
json.other do
json.array! @other_groups do |group|
json.extract! group, *UserGroup.json_keys
end
end end
# frozen_string_literal: true
# This file should contain all the record creation needed to seed the database with its default values. # This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
# #
...@@ -5,3 +6,49 @@ ...@@ -5,3 +6,49 @@
# #
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first) # Mayor.create(name: 'Emanuel', city: cities.first)
require 'factory_girl_rails'
# config.include FactoryGirl::Syntax::Methods
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean
# create named users
def create_user(first_name, last_name)
FactoryGirl.create(:confirmed_user,
first_name: first_name,
last_name: last_name)
end
john = create_user('John', 'Donnal')
john.email = 'jdonnal@gmail.com'
john.password = 'changeme'
john.save!
nicky = create_user('Nicky', 'Donnal')
steve = create_user('Steve', 'Leeb')
pete = create_user('Pete', 'Lindahl')
greg = create_user('Greg', 'Bredariol')
# create named groups
donnals = FactoryGirl.create(:user_group, name: 'Donnals', owner: john, members: [nicky])
lab = FactoryGirl.create(:user_group, name: 'Lab', owner: john, members: [steve, pete, greg])
# create real nilms
nc = CreateNilm.new
nc.run(name: 'Local', url: 'http://localhost:8080')
home = nc.nilm
FactoryGirl.create(:permission, nilm: home, user: john, role: 'admin')
FactoryGirl.create(:permission, nilm: home, user_group: donnals, role: 'owner')
FactoryGirl.create(:permission, nilm: home, user: steve, role: 'viewer')
# create fake nilms
3.times { FactoryGirl.create(:nilm, admins: [john]) }
5.times { FactoryGirl.create(:nilm, owners: [john]) }
10.times { FactoryGirl.create(:nilm, viewers: [john])}
# create other groups
5.times do
g = FactoryGirl.create(:test_user_group, size: rand(1..8))
g.users << john #john joins every group!
end
...@@ -4,22 +4,51 @@ require 'rails_helper' ...@@ -4,22 +4,51 @@ require 'rails_helper'
RSpec.describe UserGroupsController, type: :request do RSpec.describe UserGroupsController, type: :request do
let(:grp1) { create(:user_group, name: 'Group1') } let(:grp1) { create(:user_group, name: 'Group1') }
let(:grp2) { create(:user_group, name: 'Group2') } let(:grp2) { create(:user_group, name: 'Group2') }
let(:donnals) { create(:user_group, name: 'Donnals',
owner: john, members: [nicky])}
let(:john) { create(:user, first_name: 'Jonh') } let(:john) { create(:user, first_name: 'Jonh') }
let(:nicky) { create(:user, first_name: 'Nicky')}
let(:steve) { create(:user, first_name: 'Steve')}
describe 'GET index' do describe 'GET index' do
before { john.confirm } before do
john.confirm
context 'with authenticated user' do
it 'returns user groups' do
# force lazy evaluation of let to build groups # force lazy evaluation of let to build groups
grp1; grp2; grp1; grp2; donnals;
end
context 'with john' do
it 'returns 1 owner, 0 members, 2 others' do
@auth_headers = john.create_new_auth_token @auth_headers = john.create_new_auth_token
get "/user_groups.json", headers: @auth_headers get "/user_groups.json", headers: @auth_headers
expect(response.header['Content-Type']).to include('application/json') expect(response.header['Content-Type']).to include('application/json')
body = JSON.parse(response.body) body = JSON.parse(response.body)
expect(body[0]["id"]).to eq(grp1.id) expect(body["owner"].length).to eq(1)
expect(body[1]["id"]).to eq(grp2.id) expect(body["owner"][0]["members"].length).to eq(1)
expect(body["member"].length).to eq(0)
expect(body["other"].length).to eq(2)
end
end
context 'with nicky' do
it 'returns 0 owners, 1 member, 2 others' do
@auth_headers = nicky.create_new_auth_token
get "/user_groups.json", headers: @auth_headers
expect(response.header['Content-Type']).to include('application/json')
body = JSON.parse(response.body)
expect(body["owner"].length).to eq(0)
expect(body["member"].length).to eq(1)
expect(body["other"].length).to eq(2)
end
end
context 'with steve' do
it 'returns 0 owners, 0 members, 3 others' do
@auth_headers = steve.create_new_auth_token
get "/user_groups.json", headers: @auth_headers
expect(response.header['Content-Type']).to include('application/json')
body = JSON.parse(response.body)
expect(body["owner"].length).to eq(0)
expect(body["member"].length).to eq(0)
expect(body["other"].length).to eq(3)
end end
end end
context 'without sign-in' do context 'without sign-in' do
......
...@@ -5,7 +5,7 @@ FactoryGirl.define do ...@@ -5,7 +5,7 @@ FactoryGirl.define do
factory :nilm do factory :nilm do
db db
name "test_nilm" name {Faker::StarWars.unique.planet}
description { Faker::Lorem.sentence } description { Faker::Lorem.sentence }
transient do transient do
admins [] admins []
......
...@@ -8,5 +8,17 @@ FactoryGirl.define do ...@@ -8,5 +8,17 @@ FactoryGirl.define do
description { Faker::Lorem.sentence } description { Faker::Lorem.sentence }
owner { users.empty? ? create(:user): users.first } owner { users.empty? ? create(:user): users.first }
users { members.empty? ? [create(:user)] : members } users { members.empty? ? [create(:user)] : members }
factory :test_user_group do
transient do
size []
end
name { Faker::Company.unique.name }
description { Faker::ChuckNorris.fact }
owner { create(:confirmed_user) }
users { size.times.map { create(:confirmed_user) } }
end end
end
end end
...@@ -4,5 +4,11 @@ FactoryGirl.define do ...@@ -4,5 +4,11 @@ FactoryGirl.define do
last_name {Faker::Name.first_name} last_name {Faker::Name.first_name}
email {Faker::Internet.unique.email} email {Faker::Internet.unique.email}
password {Faker::Lorem.characters(10)} password {Faker::Lorem.characters(10)}
factory :confirmed_user do
confirmed_at {Time.now}
end end
end
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