Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST-assured with Spock and Groovy as integration test

Using REST-assured framework in spock framework. This is answer to closed this closed topic

like image 990
librucha Avatar asked Dec 24 '22 06:12

librucha


1 Answers

package cz.audatex.audanext.auth.endpoint.v1.member.settings

import com.jayway.restassured.RestAssured
import org.librucha.ServerApp
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.SpringApplicationConfiguration
import org.springframework.boot.test.WebIntegrationTest
import spock.lang.Specification
import spock.lang.Unroll

@SpringApplicationConfiguration(classes = ServerApp.class)
@WebIntegrationTest(randomPort = true)
class Test extends Specification {

    @Value('${local.server.port}')
    private int port

    def setup() throws Exception {
        RestAssured.port = port
    }

    @Unroll
    def "Get services as '#accessor.role' [#iterationCount]"() {
        given:
            def request = given()
                 .accept(JSON)
                 .auth().oauth2(getToken(accessor.name))
                 .log().all()
        when:
            def response = request.with().get("/api/v1/services")
        then:
            response.then().log().all()
                 .statusCode(status)
                 .spec(specification)
        where:
            accessor                                    || status       || specification
            [name: 'user-login', role: 'ordinary user'] || SC_FORBIDDEN || expect().body('code', equalTo('access_denied'), 'description', equalTo('Access is denied'))
            [name: 'admin-login', role: 'super admin']  || SC_OK        || expect().body('id', contains(1, 2, 3, 4))
    }
}
like image 147
librucha Avatar answered Jan 06 '23 01:01

librucha