Skip to content

Commit 2bae848

Browse files
committed
WIP create session
1 parent cfa796d commit 2bae848

4 files changed

Lines changed: 72 additions & 7 deletions

File tree

src/Route/Session.gren

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Db
88
import Email exposing (Email)
99
import HttpServer.Response as Response exposing (Response)
1010
import Json.Decode
11+
import Session exposing (Session)
1112
import Task exposing (Task)
1213
import User exposing (User)
1314

@@ -20,6 +21,7 @@ type Error
2021
create : Db.Connection -> Response -> Bytes -> Task Never Response
2122
create db response requestBody =
2223
findOrCreateUser db requestBody
24+
|> Task.andThen (createSession db)
2325
|> Task.andThen (\_ -> success response)
2426
|> Task.onError (\e -> failed response e)
2527

@@ -35,6 +37,12 @@ findOrCreateUser db requestBody =
3537
|> Task.mapError DbError
3638

3739

40+
createSession : Db.Connection -> User -> Task Error Session
41+
createSession db user =
42+
Session.create db user
43+
|> Task.mapError DbError
44+
45+
3846
success : Response -> Task x Response
3947
success response =
4048
Task.succeed response

src/Session.gren

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Session exposing
2+
( Session
3+
, create
4+
)
5+
6+
7+
import Db
8+
import Task exposing (Task)
9+
import User exposing (User)
10+
11+
12+
type Session =
13+
Session
14+
15+
16+
create : Db.Connection -> User -> Task Db.Error Session
17+
create db user =
18+
Debug.todo "test-drive Session.create"

src/Test/E2E/Helper.gren

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
module Test.E2E.Helper exposing
22
( expectBadStatus
3+
, expectJson
34
, initDb
45
, get
56
, post
67
, postWithJson
78
)
89

910

11+
import Bytes exposing (Bytes)
1012
import Db
1113
import Expect exposing (Expectation)
1214
import HttpClient exposing (Response)
15+
import Json.Decode
1316
import Json.Encode
1417
import Task exposing (Task)
1518

@@ -41,11 +44,11 @@ post httpPerm path =
4144
|> HttpClient.send httpPerm
4245

4346

44-
postWithJson : HttpClient.Permission -> String -> Json.Encode.Value -> Task HttpClient.Error (HttpClient.Response {})
47+
postWithJson : HttpClient.Permission -> String -> Json.Encode.Value -> Task HttpClient.Error (HttpClient.Response Bytes)
4548
postWithJson httpPerm path json =
4649
url path
4750
|> HttpClient.post
48-
|> HttpClient.expectAnything
51+
|> HttpClient.expectBytes
4952
|> HttpClient.withJsonBody json
5053
|> HttpClient.send httpPerm
5154

@@ -59,3 +62,18 @@ expectBadStatus status error =
5962
Expect.fail <|
6063
"Expected HttpClient.BadStatus, Got: " ++
6164
(Debug.toString error)
65+
66+
67+
expectJson : Json.Decode.Decoder a -> a -> Bytes -> Expectation
68+
expectJson decoder expected bytes =
69+
when Bytes.toString bytes is
70+
Nothing ->
71+
Expect.fail "Failed to decode bytes"
72+
73+
Just json ->
74+
when Json.Decode.decodeString decoder json is
75+
Ok value ->
76+
Expect.equal expected value
77+
78+
Err error ->
79+
Expect.fail (Json.Decode.errorToString error)

src/Test/E2E/Route/Session.gren

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
module Test.E2E.Route.Session exposing (tests)
22

33

4+
import Bytes
45
import Db
56
import Db.Encode
67
import Db.Decode
78
import Email
89
import Expect
910
import HttpClient
11+
import Json.Decode
1012
import Json.Encode
1113
import Task exposing (Task)
1214
import Test.Runner.Effectful exposing (Test, await, awaitError, concat, describe, test)
13-
import Test.E2E.Helper exposing (initDb, get, post, postWithJson, expectBadStatus)
15+
import Test.E2E.Helper exposing (initDb, get, post, postWithJson, expectBadStatus, expectJson)
1416
import User exposing (User)
1517

1618

@@ -62,10 +64,29 @@ tests httpPerm =
6264
[ test "Responds successfully" <| \_ ->
6365
Expect.equal 200 response.statusCode
6466

65-
, await "Getting created user from db" (getUser goodEmail) <| \user ->
66-
test "returns the created user" <| \_ ->
67-
Email.toString user.email
68-
|> Expect.equal goodEmail
67+
, await "Getting session info from db"
68+
(Db.getOne db
69+
{ query =
70+
"""
71+
select fetch_session_token from session
72+
inner join user on (user.id = session.user_id)
73+
where user.email = :email
74+
order by session.created desc
75+
limit 1
76+
"""
77+
, parameters =
78+
[ Db.Encode.string "email" goodEmail ]
79+
, decoder =
80+
Db.Decode.string "fetch_session_token"
81+
}
82+
)
83+
(\expectedToken ->
84+
test "responds with fetch session token" <| \_ ->
85+
response.data
86+
|> expectJson
87+
(Json.Decode.field "fetch_session_token" Json.Decode.string)
88+
expectedToken
89+
)
6990
]
7091
]
7192
]

0 commit comments

Comments
 (0)