I have the following code in users_controller.rb
:
def create
params = user_params
x = 1
render :json => { :message => "New user created!"}, :status => :ok
end
private
def user_params
params.permit!
end
It obviously doesn't do much right now. I don't have any before or after filters on it.
I also have the following test code:
RSpec.describe UsersController, type: :controller do
fixtures :users, :clients
include AuthHelper
before do
@request.headers["Content-Type"] = 'application/json'
@request.headers["Accept"] = 'application/json'
@default_registered_users = 3
end
describe '#create' do
context 'a user with a valid authorization token' do
before do
init_headers_for_admin
end
context 'with a valid set of input parameters' do
before do
@new_user = {
:username => 'blorp',
:email => 'blorp@blorpville.com',
:role => 'user',
:name => 'Blorp Blorperson'
}.as_json
end
it 'should create a new user' do
post :create, params: @new_user
expect(response).to have_http_status(:ok)
end
end
end
Inside of spec_helper.rb
, I have the following helper functions:
def init_headers_for_admin
# Manually added to the database, this username/password is good.
init_headers_with_login 'scottj', '771625142'
end
def init_headers_with_login (username, password)
auth_token = login username, password
init_headers_with_auth_token auth_token
end
def login (username, password)
unless @request
return login_for_integration_tests username, password
end
old_controller = @controller
@controller = UsersController.new
get_headers_for_login(username, password).each { |key, value|
@request.headers[key] = value
}
post :login
response_body = JSON.parse response.body
@controller = old_controller
response_body['auth_token']
end
def login_for_integration_tests(username, password)
headers = get_headers_for_login username, password
post '/login', headers: headers
response_body = JSON.parse response.body
response_body['auth_token']
end
def get_headers_for_login(username, password)
combined_login_password = username + ":" + password
encoded_username_password =. Base64.encode64(combined_login_password)
headers = get_default_headers
headers['X-Api-Key'] = Client.find_by_id(1234).api_key
headers['Authorization'] = 'Basic ' + encoded_username_password
headers
end
When I run the test and set a breakpoint at the code where I set x=1
, I get the following value for the params
variable: {"controller"=>"users", "action"=>"create", "user"=><ActionController::Parameters {} permitted: true>}
Notice how username
, name
, email
, and role
are not present, but a mysterious hash called user
is present, but is not populated.
The problem I'm seeing is that I can't seem to get the users_controller
to actually pass any parameters, regardless of what I do. I thought, at first, it was a problem with not permitting the correct parameters, hence the params.permit!
that I perform in users_controller
. This doesn't seem to fix the issue, though.
Can someone tell me what I'm doing wrong here? I have been banging my head against the wall for quite a while trying to figure this out.