-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_handler_test.go
119 lines (90 loc) · 2.99 KB
/
session_handler_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"bytes"
"testing"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
)
type ConnectionMock struct {
bytes.Buffer
}
func (b *ConnectionMock) Close() error {
return nil
}
func init() {
password = ""
}
func TestAuthCommand(t *testing.T) {
conn := ConnectionMock{}
_, hook := test.NewNullLogger()
storage := make(map[string]string)
handler := sessionHandler{&conn, hook.LastEntry(), storage, false}
password = "secret"
err := handler.authCommand([]string{"secret"})
assert.Nil(t, err)
assert.True(t, handler.authorized)
assert.Equal(t, "OK\n", conn.String())
conn.Reset()
err = handler.authCommand([]string{})
assert.Equal(t, "(error) ERR wrong number of arguments for 'auth' command\n", conn.String())
conn.Reset()
err = handler.authCommand([]string{"invalid"})
assert.Equal(t, "(error) ERR invalid password\n", conn.String())
assert.False(t, handler.authorized)
conn.Reset()
password = ""
err = handler.authCommand([]string{"foo"})
assert.Equal(t, "(error) ERR Client sent AUTH, but no password is set\n", conn.String())
}
func TestSetCommand(t *testing.T) {
conn := ConnectionMock{}
_, hook := test.NewNullLogger()
storage := make(map[string]string)
handler := sessionHandler{&conn, hook.LastEntry(), storage, true}
err := handler.setCommand([]string{"foo", "bar"})
assert.Nil(t, err)
assert.Equal(t, "bar", handler.storage["foo"])
assert.Equal(t, "OK\n", conn.String())
conn.Reset()
err = handler.setCommand([]string{"foo", "bar", "cat"})
assert.Equal(t, "(error) ERR wrong number of arguments for 'set' command\n", conn.String())
}
func TestGetCommand(t *testing.T) {
conn := ConnectionMock{}
_, hook := test.NewNullLogger()
storage := make(map[string]string)
handler := sessionHandler{&conn, hook.LastEntry(), storage, true}
err := handler.getCommand([]string{"foo"})
assert.Equal(t, "(nil)\n", conn.String())
storage["foo"] = "bar"
conn.Reset()
err = handler.getCommand([]string{"foo"})
assert.Nil(t, err)
assert.Equal(t, "\"bar\" \n", conn.String())
conn.Reset()
err = handler.getCommand([]string{"foo", "cat"})
assert.Equal(t, "(error) ERR wrong number of arguments for 'get' command\n", conn.String())
}
func TestPingCommand(t *testing.T) {
conn := ConnectionMock{}
_, hook := test.NewNullLogger()
handler := sessionHandler{&conn, hook.LastEntry(), nil, true}
err := handler.pingCommand([]string{})
assert.Nil(t, err)
assert.Equal(t, "PONG\n", conn.String())
conn.Reset()
err = handler.pingCommand([]string{"foo", "cat"})
assert.Equal(t, "(error) ERR wrong number of arguments for 'ping' command\n", conn.String())
}
func TestNoAuth(t *testing.T) {
conn := ConnectionMock{}
_, hook := test.NewNullLogger()
password = "secret"
handler := sessionHandler{&conn, hook.LastEntry(), nil, false}
err := handler.setCommand([]string{"foo", "bar"})
assert.Equal(t, ErrAuth, err)
err = handler.getCommand([]string{"foo"})
assert.Equal(t, ErrAuth, err)
err = handler.pingCommand([]string{})
assert.Equal(t, ErrAuth, err)
}