How to test with rest assured when post method performs both delete and put with payload - rest-assured

I need help in writing rest assured script for below scenarios
Base_URI = http://localhost/TestManager/ServiceApi
Delete is achieved with : POST/User/{Id}/Delete call.
Put is achieved with : POST/User with payload.

Follow basic principles in the tutorial - https://www.guru99.com/rest-assured.html

I think there are 3 API one to create user, Update the user details and Delete the user
create a Map object of all the parameters used for crateing a user
ValidatableResponse createResposne = given().formParams(mapObject).when().post(URI).then().log().all();
extract the id from this API(using JSON path) and use the same id to delete the user or update the user
ValidatableResponse deleteResponse = given().formParams(mapObject).when().post(/User/{Id}/Delete call.).then().log().all();

Related

Ruby on rails api post request issue

i am trying to post using postman to a rails api that i made, the actual request goes in and creates an entry, but nothing but the ID gets recorded. attached are the files for that.
You need to pass the post params and not just the id into the list.new call and make sure you're sending up the correctly namespaced values in the post request.
Step 1.
In create you need to do
#list = List.new(list_params)
Step 2.
Postman needs to be putting all the params into the list[] namespace
ie. list[title] rather than just title.

Spree: how to get the current order for a user without their session?

I'm looking for a way to get the current order for a particular user within a webhook controller so that I can apply a promotion to it. Since it's a webhook controller, I won't have access to the session for the user whose order I need, so I won't be able to use the current_order method within Spree::Core::ControllerHelpers::Order . I'm using spree 2.2.
This logic may provide what you're looking for:
https://github.com/spree/spree/blob/11a24a823780dbc63708b9ba840f4e7696202dc2/core/config/initializers/user_class_extensions.rb#L22-L24
You could also pass the Spree::Order ID along to your webhook controller, and just use that to find the order.

Rails 4 — How to populate a user model from JSON API?

Firstly, I am new to rails so sorry if there is anything that I don't understand correctly. I am wondering how can I populate a model with data fetch thru an API.
Context: I am using a OAuth2 authentication with omniauth/devise.
In my User controller client-side (opposite to the provider), I fetched all users who logged in at least once is this "client app" and I want to display them. Obviously, anytime a new user logged in to the client app I don't store all his information in the client database to avoid duplicate. All I am storing is the user_id along with its access token.
Therefore, I was thinking that after fetching all users data I could populate them to a user model before passing it to the view. What would be the best way of doing such a thing?
I was looking into the Named Scope but it is not clear to me how to apply it to my particular scenario. For instance, it would be great to be able to fetch an populate all users in my model using something like this:
# ApiRequest is a container class for HTTParty
get = ApiRequest.new.get_users(User.all) // here "get" is a JSON array of all users data
response = get.parsed_response['users']
User.populate(response).all # would something like this possible? If yes, is Named Scope the appropriate solution?
Thank you very much in advance for you help.
Let's say that response is an array of attribute hashes:
[{'id' => 1, 'name' => 'Dave'}, {'id' => 2, 'name' => 'Bill'}]
You can map this into an array of Users with:
users = response.map{|h| User.new(h)}
If you don't want to touch database, and also want to populate virtual attributes I think the only way is to implement your own populate method:
# The User class
def self.populate(data)
data.map do |user_json|
user = User.find(user_json[:id])
user.assign_attributes(user_json)
user
end
end
Check the assign_attributes documentation for security advices.
The simpliest way is to use active_resource
http://railscasts.com/episodes/94-activeresource-basics
P.S. In Rails 4 it's gone to gem https://github.com/rails/activeresource
I don't find a direct way to do it, the shortest solution I can suggest is using the method update:
ids = get.map{ |e| e["id"] }
User.update(ids, get)

Does self as identifier can represent the resource on restful api?

I'm working on REST API, and I trying to understand whether this looks legit in terms of REST.
I've players which using some mobile app with a login mechanism,
So the question is, if the player needs to update some attribute on his resource,
How the URL & PARAMS should looks like:
Option #1:
PUT /api/players/59/
PARAMS { some_attribute: "some_value" }
Option #2:
PUT /api/players/self
PARAMS { some_attribute: "some_value" }
The thing is, that the player doing the call with authentication, so it looks odd that he needs to send his id, it feels like he can send update on some other id, so when he sends 'self' it looks more suitable but uglier.
What's the REST point of view here?
Or maybe another option?
You should use first option
But in first option, request additional parameter like authentication_token
to verify the user and user is updating his information only.
This gist will help you in authenticating user
https://gist.github.com/josevalim/fb706b1e933ef01e4fb6
It shows 2 method for authenticating user while communicating with API

Which HTTP method should I use for request that can create or simply read a resource?

In my Rails application I have an url routed to an action in charged of showing or creating (if not existing) e resource. What is the appropriate http verb to use for this kind of request?
To be more precise, in my method I don't directly access the resource but I use a library which has that behavior: first search and then create the resource if not exiting. My method, in the end, always provide the resource returned by the library either a brand new one or an old one. Hence I cannot split into two requests.
According to this and considering my method always returns the same resource (idempotent) it seems that PUT should be the right one. I just wonder whether PUT can be used in case where e resource is actually just retrieved (get) and anything is not even updated
tnx
POST for creating, GET for showing is automatically used by rails. But I hope you can do all sorts of things with custom programming as data will be available to you in form of params[]
According to Ruby on Rails guides, you should use GET and POST verbs. More information here: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
You use GET to retrieve.
If resource found return 200 with resource.
If resource not found let it return 404 and check the error code and use POST and create the resource.
If you donot need any parameter while creating resource then you should use GET request Else if you need params while creating resource , then you should make separate action for creating(Post request with params) and showing(GET request) resource.

Resources