Permalink
Browse files

Added current user and meetings.

  • Loading branch information...
dblock committed Jul 7, 2018
1 parent 52a2492 commit 8382006996c82eb267f25854739487f2e26bedc3
@@ -1,20 +1,27 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-07-07 15:37:08 -0400 using RuboCop version 0.57.2.
# on 2018-07-07 17:07:40 -0400 using RuboCop version 0.57.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 3
# Offense count: 2
Style/DateTime:
Exclude:
- 'spec/graphql/mutations/create_meeting_mutation_spec.rb'

# Offense count: 7
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: line_count_dependent, lambda, literal
Style/Lambda:
Exclude:
- 'app/graphql/mutations/create_meeting_mutation.rb'
- 'app/graphql/mutations/create_user_mutation.rb'
- 'app/graphql/mutations/login_mutation.rb'
- 'app/graphql/mutations/logout_mutation.rb'
- 'app/graphql/types/query_type.rb'

# Offense count: 2
Style/MixinUsage:
@@ -0,0 +1,23 @@
Mutations::CreateMeetingMutation = GraphQL::Relay::Mutation.define do
name 'createMeeting'

input_field :title, types.String
input_field :started, !Types::DateTimeType
input_field :finished, !Types::DateTimeType

return_field :meeting, Types::MeetingType

resolve ->(_object, inputs, ctx) {
if ctx[:current_user]
meeting = ctx[:current_user].meetings.create!(
title: inputs[:title],
started_at: inputs[:started],
finished_at: inputs[:finished]
)

{ meeting: meeting }
else
GraphQL::ExecutionError.new('Not logged in.')
end
}
end
@@ -0,0 +1,10 @@
Types::MeetingType = GraphQL::ObjectType.define do
name 'Meeting'
description 'A recorded meeting.'

field :id, types.ID, 'Meeting ID.'
field :title, types.String, 'Meeting title.'
field :started, Types::DateTimeType, 'The time at which this meeting started.', property: :started_at
field :finished, Types::DateTimeType, 'The time at which this meeting ended.', property: :finished_at
field :user, Types::UserType, 'Owner of this meeting.'
end
@@ -4,4 +4,6 @@
field :createUser, field: Mutations::CreateUserMutation.field
field :login, field: Mutations::LoginMutation.field
field :logout, field: Mutations::LogoutMutation.field

field :createMeeting, field: Mutations::CreateMeetingMutation.field
end
@@ -1,4 +1,34 @@
Types::QueryType = GraphQL::ObjectType.define do
name 'Query'
description 'The query root of this schema. See available queries.'

field :current_user, !Types::UserType do
description 'Get current user.'
resolve ->(_obj, _args, ctx) {
ctx[:current_user] || GraphQL::ExecutionError.new('Not logged in.')
}
end

field :meeting, !Types::MeetingType do
argument :id, !types.String
description 'Get a meeting by ID.'
resolve ->(_obj, args, ctx) {
if ctx[:current_user]
ctx[:current_user].meetings.where(_id: args[:id]).first
else
GraphQL::ExecutionError.new('Not logged in.')
end
}
end

field :meetings, !types[Types::MeetingType] do
description 'Get current user meetings.'
resolve ->(_obj, _args, ctx) {
if ctx[:current_user]
ctx[:current_user].meetings
else
GraphQL::ExecutionError.new('Not logged in.')
end
}
end
end
@@ -6,4 +6,6 @@
field :name, types.String, 'User full name.'
field :email, types.String, 'User email.'
field :created_at, Types::DateTimeType, 'Account creation date.'

field :meetings, -> { !types[Types::MeetingType] }
end
@@ -0,0 +1,23 @@
class Meeting
include Mongoid::Document
include Mongoid::Timestamps

field :title, type: String
field :started_at, type: DateTime
field :finished_at, type: DateTime

validates_presence_of :started_at
validates_presence_of :finished_at
validate :check_times

belongs_to :user

index({ user_id: 1, started_at: 1, finished_at: 1 }, unique: true)

private

def check_times
return unless started_at && finished_at
errors.add(:meeting, 'Meeting cannot finish before it started.') if started_at > finished_at
end
end
@@ -23,6 +23,8 @@ def authenticate(password)
return self if BCrypt::Password.new(password_digest) == password
end

has_many :meetings

protected

def encrypt_password
@@ -0,0 +1,5 @@
Fabricator(:meeting) do
started_at { Faker::Time.backward(1) }
finished_at { Time.now }
user { Fabricate(:user) }
end
@@ -0,0 +1,44 @@
require 'rails_helper'

describe 'Create Meeting', type: :request do
include_context 'Authenticated Client'

let(:query) do
<<-GRAPHQL
mutation($input: createMeetingInput!) {
createMeeting(input: $input) {
meeting {
id
title
started
finished
}
}
}
GRAPHQL
end

let(:title) { Faker::Company.buzzword }
let(:started_at) { Faker::Time.backward(1) }
let(:finished_at) { Time.now }

it 'returns an meeting' do
expect do
response = client.execute(
query, input: {
title: title,
started: started_at,
finished: finished_at
}
)
meeting = response.data.create_meeting.meeting
expect(meeting.title).to eq title
expect(DateTime.parse(meeting.started)).to eq started_at.utc.iso8601
expect(DateTime.parse(meeting.finished)).to eq finished_at.utc.iso8601

user_meeting = current_user.meetings.first
expect(user_meeting).to_not be nil
expect(user_meeting.id.to_s).to eq meeting.id
end.to change(Meeting, :count).by(1)
end
end
@@ -0,0 +1,22 @@
require 'rails_helper'

describe 'Current User Query', type: :request do
include_context 'Authenticated Client'

let(:query) do
<<-GRAPHQL
query {
current_user {
id
email
}
}
GRAPHQL
end

it 'returns an current_user' do
response = client.execute(query)
user = response.data.current_user
expect(user.id).to eq current_user.id.to_s
end
end
@@ -0,0 +1,26 @@
require 'rails_helper'

describe 'Current User Meeting Query', type: :request do
include_context 'Authenticated Client'

let(:query) do
<<-GRAPHQL
query($id: String!) {
meeting(id: $id) {
id
title
started
finished
}
}
GRAPHQL
end

let!(:meeting) { Fabricate(:meeting, user: current_user) }

it 'returns the meeting' do
response = client.execute(query, id: meeting.id.to_s)
meeting = response.data.meeting
expect(meeting.id).to eq meeting.id.to_s
end
end
@@ -0,0 +1,25 @@
require 'rails_helper'

describe 'Current User Meetings Query', type: :request do
include_context 'Authenticated Client'

let(:query) do
<<-GRAPHQL
query {
meetings {
id
title
started
finished
}
}
GRAPHQL
end

let!(:meetings) { 3.times { Fabricate(:meeting, user: current_user) } }

it 'returns the meetings' do
meetings = client.execute(query).data.meetings
expect(meetings.size).to eq 3
end
end
@@ -0,0 +1,4 @@
require 'rails_helper'

RSpec.describe Meeting, type: :model do
end
@@ -0,0 +1,29 @@
require 'graphlient'

RSpec.shared_context 'Authenticated Client', shared_context: :metadata do
include_context 'GraphQL Client'

let(:password) { 'password' }
let(:current_user) { Fabricate(:user, password: password) }

let(:login_query) do
<<-GRAPHQL
mutation($input: loginInput!) {
login(input: $input) {
user {
id
}
}
}
GRAPHQL
end

before do
client.execute(
login_query, input: {
email: current_user.email,
password: password
}
)
end
end

0 comments on commit 8382006

Please sign in to comment.