Test UrlMapping Paths for Resource Controllers - grails

In Grails 2.3 and above, there is now the concept of a resource controller. However, they do not seem to work with the assertUrlMappings concepts.
/foo (resources:"foo")
Then in the url-mappings-report the following appears:
Controller: foo
| GET | /foo | Action: index
| GET | /foo/create | Action: create
| POST | /foo | Action: save
| GET | /foo/${id} | Action: show
| GET | /foo/${id}/edit | Action: edit
| PUT | /foo/${id} | Action: update
| PATCH | /foo/${id} | Action: patch
| DELETE | /foo/${id} | Action: delete
However, in the following file, the urls cannot be tested:
#TestFor(UrlMappings)
#Mock(FooController)
class FooURIReverseMappingsSpec extends Specification {
def "Ensure basic mapping operations for Foo uris"() {
expect:
assertUrlMapping(url, controller: expectCtrl, action: expectAction) {
id = expectId
}
where:
url | expectCtrl | expectAction | expectId
// Not sure why, but the resource controller generation url test does not find the real 'Foos' url
'/foo/123'|'foo'|'show'|'123'
}
}
Then WITHOUT the default action url $controller/$action/$id (which would match foo/show/123) the test cannot find the generated url.
The generated error is:
junit.framework.AssertionFailedError: url '/foo/123' did not match any mappings
Is there a way to test the resource urls?

I know this question is over 5 years old, but for future reference, the issue likely was the same as that in this grails-core Github issue (URLMapping unit test for RESTfull service is not working).
The issue was apparently solved by this PR (Include request method in URL matching). It looks like this fix was merged into Grails 2.5.x.

Related

Mosquitto >1.5 User Access Level Wildcard

I am trying to use Mosquitto MQTT Broker v1.5.8.
I am using mosquitto_auth_plugin for User Authentication. (https://github.com/jpmens/mosquitto-auth-plug)
I created a mysql server with users and acls tables.
I want to setup a user that can subscribe to topic test/#.
So I setup the rw = 5 in the acls table for that user
However the user is not able to subscribe to any test/# but can subscribe to test/123
I looked at one of the issue posted https://github.com/jpmens/mosquitto-auth-plug/issues/356
but since the Repo is archived, I cant ask questions there.
mysql> select * from acls;
+----+--------------+--------------------+----+
| id | username | topic | rw |
+----+--------------+--------------------+----+
| 1 | test | test/# | 5 |
+----+--------------+--------------------+----+

specflow hooks between two given steps

i have the following specflow test:
#done
#succesfulladdbeforewhen
Scenario Outline: Admin adds an alternate numbers entry with a phonenumber successfully
Given the BW User is logged in with a <userlevel> Level Account
And the AddAlternateNumbersEntry query parameter contains an existing userid within the scope of the BW User
And the AddAlternateNumbersEntry post body contains an available phonenumber
When the BW User submits the AddAlternateNumbersEntry request
Then HTTP 201 should be returned
And a new alternate numbers entry should be added
Examples: User Levels
| userlevel |
| System |
| Service Provider |
| Group |
However i want a hook between before the second "and" (And the AddAlternateNumbersEntry post body contains an available phonenumber)
How can i do this ??? i can't find the propper kind of code/hook to do it with. thnx for the advice
thnx

I want to write script for login functionality. There are 5 types of user for that I have written following feature file

Query: I want to login with different user for that I am parametrizing the Usertype and will verify the respective element w.r.t there access.
Now In Step file suppose eg: I have written
#Then ("^User logged in with \"([^\"]*)\"$)
Public void User_logged_in_with_Usertype() {
If (Usertype= Admin){
...
So in above code in how I will get value of admin or any other user(Can we get same value from feature file or I need to write a code separately for each user)
Feature: As a user I would like to login in FMJ-Redesign application with different users
Story : User is logging in FMJ application
Scenario Outline: User is logged in with Admin user credentials
Given Navigating BU to "<Browser>"
When User clicks on Location
And ForevermarkJewellerWebsite element should be present on login page
Then User logged in with "<Usertype>"
And User will check visibility of "<Element>"
Then User Logout Successfully
Examples:
|Browser | UserType | Element |
|Chrome | Admin | |
|Chrome | Market | |
|Chrome | Jeweller | |
|Chrome | Store | |
The way to get the value for Usertype is to have it as a parameter in the method.
Your code looks like this:
#Then ("^User logged in with \"([^\"]*)\"$)
Public void User_logged_in_with_Usertype() {
If (Usertype= Admin){
...
I rewrote your Scenario outline a bit and ended up with this version
Scenario Outline: User is logged in with Admin user credentials
Given Navigating BU to <Browser>
When User clicks on Location
And ForevermarkJewellerWebsite element should be present on login page
Then User logged in with <UserType>
And User will check visibility of <Element>
Then User Logout Successfully
Examples:
| Browser | UserType | Element |
| Chrome | Admin | |
| Chrome | Market | |
| Chrome | Jeweller | |
| Chrome | Store | |
This allowed me to write the step like this:
#Then("^User logged in with (.*)$")
public void user_logged_in_with(String userType) throws Throwable {
if (userType.equals("Admin")) {
// implement our behaviour here
}
}
There are a few differences to notice here:
I removed the quotes around the parameters in the example
I added the value of UsertType as a parameter to the method user_logged_in_with
These changes simplified the regular expression needed and allows you to implement different behaviour for different UserTypes.
I would probably implement four different steps for the user types you have. This would give me simpler steps, I could avoid the condition. The price would be four methods.
If I did that, the resulting implementation would look like this instead:
#Then("^User logged in with Admin$")
public void user_logged_in_with_Admin() throws Throwable {
// implement our behaviour here
}
This simplified the regular expression even more and removed the need for a capture group.

How to write a test in cucumber?

This is my first test case with cucumber and I am having tough time to figure out one thing. So I already have a test which check user creation. I want to add more steps like on user create create account and create product and set some attributes to these objects.
Here is the user create feature file
#no-database-cleaner
Feature: Creating an user account
In order to use Recdns portal
I need to create an user account
Scenario: Successful user account creation
Given the database contains no test data
And I am on the homepage
When I attempt to create the following user account:
| email address | password | confirm password |
| abc#company1.com | password | password |
# User is automatically logged in
Then I should see "Welcome!" message on page
And an ar_id is set for the user
And
When I click "Sign Out"
Then I should see "Signed out"
When I attempt to sign in with following user account:
| email address | password |
| abc#company1.com | password |
Then I should see "Welcome!" message on page
Scenario Outline: Verify error message when user failed to sign in
Given there is a user
And I am on the homepage
And I try to login with email "<email address>", password "<password>"
Then I should see "<error message>" message on page
Examples:
| email address | password | error message |
| testuser#company1.com | abc1234 | Invalid email or password |
| testuserdoesnotexist#company1.com | password | Invalid email or password |
Scenario Outline: Verify error message when user failed to sign up
Given I am on the homepage
And I click "Sign Up"
And I try to create user with email "<email address>", password "<password>", confirm password "<confirm password>"
Then I should see "<error message>" message on page
Examples:
| email address | password | confirm password | error message |
| abc#company1.com | abc123 | abc123 | Email has already been taken |
| xyz#test | abc123 | abc123 | Email is invalid |
| | abc123 | abc123 | Email can't be blank |
| abc#company1.com | | abc123 | Password can't be blank |
| abc#company1.com | abc123 | | Password doesn't match confirmation |
| abc#company1.com | abc1 | abc1 | Password is too short |
So where exactly I can add steps saying create account table and product table. Thanks
Is your 'create user account' method something that you need for each test to run? If so, I'd just create a helper that you can call before your scenario. ..something like #needs_new_account. One of the best parts about cucumber is the ability to reuse environments. You probably shouldn't create a new account for each scenario. Instead, have cucumber remember that it needs to keep that new account (ie., dont delete it) and reuse for the rest of your scenarios, if possible. Let me know if this helps. ..not quite sure I'm understanding your question completely.
Also, you don't need your extra "and" between "and" and "when"...Gherkin syntax treats And/When/Then the same.

Specflow not passing table

I've just started playing around with Specflow, Watin and Deleporter.
I am having a problem with an integration type test, in that it wont pass the table data into the corresponding test.
Scenario: User tries to register and their email already exists
Given I have filled out the register page with the following data
| Name | Password | Nickname | Email |
| Sam | password1 | SamsNickname | email#email.com |
When I already have an account with the same email
| Name | Password | Nickname | Email |
| Sam | password1 | SamsNickname | email#email.com |
Then Show message that the account exists
[Given(#"I have filled out the register page with the following data")]
public void GivenIHaveFilledOutTheRegisterPageWithTheFollowingData(Table table)
{
var data = table.Rows.First();
}
The problem I am having is the Table parameter above is passed in as null. Is there something I'm missing here?
The wrong runner executes your test.
Go to the following option:
Tools -> Options -> Specflow -> General -> Test Runner Tool
Change the value to "Auto" or "SpecRun"

Resources