TESTING

TestBox logo

ColdBox is the ONLY CFML framework that offers integration and behavior-driven testing through TestBox. By using TestBox, developers can automate tests in order to ensure that the code they write is functioning as expected.

This can save a significant amount of time and effort in the long run, as issues can be caught and fixed before they become major problems.

In addition, ColdBox allows developers to test their applications without even using a browser, which can be a huge advantage in speed and convenience. By using ColdBox with TestBox, developers can streamline their workflow and ensure that their code is robust and reliable.

install testbox
    story( "I want to authenticate a user via username/password and receive a JWT token", () => {
    given( "a valid username and password", () => {
        then( "I will be authenticated and will receive the JWT token", () => {
            // Use a user in the seeded db
            var event = this.post(
                "/api/v1/login",
                {
                    username : variables.testEmployeeEmail,
                    password : variables.testPassword
                }
            )
            var response = event.getPrivateValue( "Response" )
            expect( response.getError() ).toBeFalse( response.getMessagesString() )
            expect( response.getData() ).toHaveKey( "token,user" )

            // debug( response.getData() )

            var decoded = jwt.decode( response.getData().token )
            expect( decoded.sub ).toBe( variables.testEmployeeId )
            expect( decoded.exp ).toBeGTE( dateAdd( "h", 1, decoded.iat ) )
            expect( response.getData().user.userId ).toBe( variables.testEmployeeId )
        })
    })

    given( "invalid username and password", () => {
        then( "I will receive a 401 invalid credentials exception ", () => {
            var event = this.post(
                "/api/v1/login",
                { username : "invalid", password : "invalid" }
            )
            var response = event.getPrivateValue( "Response" )
            expect( response.getError() ).toBeTrue()
            expect( response.getStatusCode() ).toBe( 401 )
        })
    })

    given( "an invalid token", () => {
        then( "I should get an error", () => {
            // Now Logout
            var event = GET(
                "/api/v1/whoami",
                { "x-auth-token" : "123" }
            )
            expect( event.getResponse() ).toHaveStatus( 500 )
         })
     })
   })