Last active
May 5, 2020 14:48
-
-
Save sankalpk/ae1a9c86d30bc4303ef342b1c6b1f20d to your computer and use it in GitHub Desktop.
Create User Mutation Spec Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'rails_helper' | |
RSpec.describe Mutations::CreateUser, 'create user mutation' do | |
before(:all) do | |
@basic_mutation = <<-GRAPHQL | |
mutation CreateUser($firstName: String!, $lastName: String!, $ssn: String, $dob: ISO8601Date!) { | |
createUser(firstName: $firstName, lastName: $lastName, ssn: $ssn, dob: $dob) { | |
user { | |
id | |
} | |
} | |
} | |
GRAPHQL | |
end | |
let(:first_name) { Faker::Name.first_name } | |
let(:last_name) { Faker::Name.last_name } | |
let(:dob) { 30.years.ago.to_date.iso8601 } | |
let(:ssn) { (Faker::Number.number(9)).insert(3, '-').insert(6, '-') } | |
let(:variables) { { firstName: first_name, lastName: last_name, dob: dob, ssn: ssn } } | |
let(:context) { build(:admin_user_context, roles: ['Manage_Users']) } | |
it 'can create a user and return it' do | |
result = ApiSchema.execute( | |
@basic_mutation, | |
context: context, | |
variables: variables | |
).to_h | |
returned_user_id = result.dig('data', 'createUser', 'user', 'id').to_i | |
expect(returned_user_id).to be_present | |
user = User.last | |
expect(user.profile.first_name).to eq(first_name) | |
expect(user.profile.last_name).to eq(last_name) | |
expect(user.profile.dob.iso8601).to eq(dob) | |
expect(user.profile.ssn).to eq(ssn) | |
email_beginning = Regexp.quote("#{first_name.downcase}.#{last_name.downcase}") | |
email_domain = Regexp.quote(ENV['BACKEND_USERS_DOMAIN']) | |
email_regex = /\A#{email_beginning}.+#{email_domain}\z/ | |
expect(user.email).to match(email_regex) | |
end | |
it 'can not create a user if admin does not have user management permission' do | |
no_permission_context = build(:admin_user_context, roles: []) | |
result = ApiSchema.execute( | |
@basic_mutation, | |
context: no_permission_context, | |
variables: variables | |
).to_h | |
expect(result['errors']).to be_present | |
end | |
it 'can not create a user if a regular user' do | |
user_context = build(:user_context) | |
result = ApiSchema.execute( | |
@basic_mutation, | |
context: user_context, | |
variables: variables | |
).to_h | |
expect(result['errors']).to be_present | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
FactoryBot.define do | |
factory :admin_user_context, class: Hash do | |
transient do | |
roles { AuthenticateAdmin::VALID_ROLES.sample(2) } | |
end | |
admin_user { | |
{ | |
'unique_name' => Faker::Internet.email, | |
'roles' => roles | |
} | |
} | |
initialize_with { attributes } | |
end | |
factory :user_context, class: Hash do | |
current_user { | |
FactoryBot.create(:user) | |
} | |
initialize_with { attributes } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment