forked from knadh/niltalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
77 lines (63 loc) · 1.49 KB
/
api_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Niltalk, April 2015
// https://niltalk.com
// https://github.com/goniltalk/niltalk
// License AGPL3
package main
import (
"testing"
"time"
"github.com/alexedwards/stack"
)
func TestLogin(t *testing.T) {
// Setup the room.
room_id := "testroom"
pw := "password"
err := newRoom(room_id, []byte(pw))
if err != nil {
t.Fatalf("Couldn't create testroom, %v", err)
}
room, err := initializeRoom(room_id)
if err != nil || room == nil || room.Id != room_id {
t.Fatalf("Couldn't initialize testroom, %v", err)
}
ctx := &stack.Context{Room: room}
}
func TestGenerateRoomId(t *testing.T) {
room_id, err := generateRoomId(5)
if err != nil {
t.Fatal("Couldn't generate room id")
}
if len(room_id) != 5 {
t.Fatalf("Invalid room id length. 5 != %d", len(room_id))
}
room_id, err = generateRoomId(10)
if err != nil {
t.Fatal("Couldn't generate room id")
}
if len(room_id) != 10 {
}
}
func TestSessions(t *testing.T) {
r := &Room{
Id: "testroom",
password: []byte("test"),
timestamp: time.Now(),
}
token, err := newSession(r)
if err != nil {
t.Fatalf("Couldn't register session, %v", err)
} else {
if len(token) != 32 {
t.Fatalf("Invalid token string (not len 32), %s", token)
}
}
// See if validation works.
if validateSessionToken(r, token) != true {
t.Fatal("Couldn't validate newly registered session token")
}
// Clear the session.
clearSessions(r)
if validateSessionToken(r, token) == true {
t.Fatalf("Cleared session still persists in the db")
}
}