Skip to content

Commit 5284c30

Browse files
committed
Add TestAuthTests in Kotlin
1 parent 630cb87 commit 5284c30

File tree

2 files changed

+98
-8
lines changed

2 files changed

+98
-8
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package test.servicestack.net.kotlin.test
2+
3+
import junit.framework.Assert
4+
import junit.framework.TestCase
5+
import net.servicestack.client.JsonServiceClient
6+
import net.servicestack.client.WebServiceException
7+
8+
/**
9+
* Created by mythz on 1/3/2016.
10+
*/
11+
class TestAuthTests : TestCase() {
12+
13+
fun CreateClient(): JsonServiceClient {
14+
return JsonServiceClient("http://test.servicestack.net")
15+
}
16+
17+
fun test_AuthRequired_returns_401() {
18+
try {
19+
val client = CreateClient()
20+
client.get(TestAuth())
21+
Assert.fail("should throw")
22+
} catch (ex: WebServiceException) {
23+
Assert.assertEquals(401, ex.statusCode)
24+
Assert.assertEquals("Unauthorized", ex.statusDescription)
25+
}
26+
}
27+
28+
fun test_does_send_BasicAuthHeaders() {
29+
val client = CreateClient()
30+
client.setCredentials("test", "test")
31+
client.alwaysSendBasicAuthHeaders = true
32+
33+
val response = client.get(TestAuth())
34+
35+
assertEquals("1", response.userId)
36+
assertEquals("test", response.userName)
37+
assertEquals("test DisplayName", response.displayName)
38+
Assert.assertNotNull(response.sessionId)
39+
}
40+
41+
fun test_does_transparently_send_BasicAuthHeader_on_401_response() {
42+
val client = CreateClient()
43+
client.setCredentials("test", "test")
44+
45+
val response = client.get(TestAuth())
46+
47+
assertEquals("1", response.userId)
48+
assertEquals("test", response.userName)
49+
assertEquals("test DisplayName", response.displayName)
50+
Assert.assertNotNull(response.sessionId)
51+
}
52+
53+
fun test_can_authenticate_with_CredentialsAuth() {
54+
val client = CreateClient()
55+
56+
var request = Authenticate()
57+
request.provider = "credentials"
58+
request.userName = "test"
59+
request.password = "test"
60+
61+
val authResponse = client.post(request)
62+
63+
Assert.assertEquals("1", authResponse.userId)
64+
Assert.assertEquals("test", authResponse.userName)
65+
Assert.assertNotNull(authResponse.sessionId)
66+
67+
val response = client.get(TestAuth())
68+
69+
assertEquals("1", response.userId)
70+
assertEquals("test", response.userName)
71+
assertEquals("test DisplayName", response.displayName)
72+
Assert.assertNotNull(response.sessionId)
73+
}
74+
}

src/AndroidClient/kotlin/src/androidTest/java/test/servicestack/net/kotlin/test/dtos.kt

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Options:
2-
Date: 2015-12-09 03:53:30
2+
Date: 2016-01-13 04:11:23
33
Version: 4.00
44
Tip: To override a DTO option, remove "//" prefix before updating
55
BaseUrl: http://test.servicestack.net
@@ -261,8 +261,8 @@ open class HelloExternal
261261
}
262262

263263
/**
264-
* AllowedAttributes Description
265-
*/
264+
* AllowedAttributes Description
265+
*/
266266
@Route(Path="/allowed-attributes", Verbs="GET")
267267
@Api("AllowedAttributes Description")
268268
// @ApiResponse(400, "Your request was not understood")
@@ -348,8 +348,8 @@ open class HelloWithDataContract : IReturn<HelloWithDataContractResponse>
348348
}
349349

350350
/**
351-
* Description on HelloWithDescription type
352-
*/
351+
* Description on HelloWithDescription type
352+
*/
353353
open class HelloWithDescription : IReturn<HelloWithDescriptionResponse>
354354
{
355355
var name:String? = null
@@ -534,6 +534,13 @@ open class UpdateSession : IReturn<GetSessionResponse>
534534
override fun getResponseType(): Any? = responseType
535535
}
536536

537+
@Route("/testauth")
538+
open class TestAuth : IReturn<TestAuthResponse>
539+
{
540+
companion object { private val responseType = TestAuthResponse::class.java }
541+
override fun getResponseType(): Any? = responseType
542+
}
543+
537544
@Route("/void-response")
538545
open class TestVoidResponse
539546
{
@@ -861,8 +868,8 @@ open class HelloWithDataContractResponse
861868
}
862869

863870
/**
864-
* Description on HelloWithDescriptionResponse type
865-
*/
871+
* Description on HelloWithDescriptionResponse type
872+
*/
866873
open class HelloWithDescriptionResponse
867874
{
868875
var result:String? = null
@@ -930,6 +937,15 @@ open class GetSessionResponse
930937
var responseStatus:ResponseStatus? = null
931938
}
932939

940+
open class TestAuthResponse
941+
{
942+
var userId:String? = null
943+
var sessionId:String? = null
944+
var userName:String? = null
945+
var displayName:String? = null
946+
var responseStatus:ResponseStatus? = null
947+
}
948+
933949
@DataContract
934950
open class RequestLogsResponse
935951
{
@@ -1436,4 +1452,4 @@ open class MenuItemExampleItem
14361452
@DataMember(Order=1)
14371453
@ApiMember()
14381454
var name1:String? = null
1439-
}
1455+
}

0 commit comments

Comments
 (0)