-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.go
69 lines (61 loc) · 1.91 KB
/
service_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
package main
import "testing"
import "net/http"
import "net/url"
import "strings"
import "net/http/httptest"
import "github.com/jameskeane/bcrypt"
import "github.com/vmihailenco/redis"
var server *httptest.Server
func InitTests() {
server = httptest.NewServer(createRouter())
connectToRedis = func() *redis.Client {
// CONFIGURE THIS!
password := ""
client := redis.NewTCPClient("localhost:6379", password, 1)
return client
}
client := connectToRedis()
defer client.Close()
client.FlushDb()
// Bootstrap the system, with an admin
hash, _ := bcrypt.Hash("asdf")
client.HMSet("user:admin", "isadmin", "1", "password", hash)
}
func TestCreateUserNoAuth(t *testing.T) {
InitTests()
resp, err := http.PostForm(server.URL + "/u/gino/",
url.Values{"password": {"berlino"}, "isadmin": {"1"}})
if err != nil {
t.Errorf("%v", err)
}
if resp.StatusCode != 403 {
t.Errorf("Server responded with %v, expected 403", resp.StatusCode)
t.FailNow()
}
client := connectToRedis()
defer client.Close()
if client.Exists("user:gino").Val() {
t.Errorf("User was created without permission")
}
}
func TestCreateUser(t *testing.T){
InitTests()
req, _ := http.NewRequest("POST", server.URL + "/u/gino/", strings.NewReader(url.Values{"password": {"berlino"}, "isadmin": {"1"}}.Encode()))
req.SetBasicAuth("admin", "asdf")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Errorf("%v", err)
}
if resp.StatusCode != 200 {
t.Errorf("Server responded with %v, expected 200", resp.StatusCode)
t.FailNow()
}
client := connectToRedis()
defer client.Close()
v := client.HGetAll("user:gino").Val()
if !(bcrypt.Match("berlino", v[3])) {
t.Errorf("%v v1 is %v and v2 is %v %v", v[0], v[1], v[2], v[3])
t.FailNow()
}
}