Skip to content
This repository has been archived by the owner on Apr 20, 2019. It is now read-only.

Commit

Permalink
unit tests for authkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe committed Feb 21, 2015
1 parent e3d9920 commit 2101ce4
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 42 deletions.
29 changes: 12 additions & 17 deletions authkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@ package main

import "github.com/fzzy/radix/redis"

type AuthKeys struct {
Domains map[string]string
}

func (ak *AuthKeys) Init() {
ak.Domains = make(map[string]string)
}
func (ak *AuthKeys) GetDomain(key string, conn *redis.Client) (string, error) {
if domain, ok := ak.Domains[key]; ok {
return domain, nil
func GetDomain(key string, conn *redis.Client) (string, error) {
reply := conn.Cmd("HGET", "$domainkeys", key)
if reply.Type == redis.NilReply {
return "", reply.Err
}
domain, err := reply.Str()
if err != nil {
return "", err
} else {
domain, err := conn.Cmd("HGET", "$domainkeys", key).Str()
if err != nil {
return "", err
}
// now cache the value
// perhaps we should remember this with a timestamp
ak.Domains[key] = domain
return domain, nil
}
}

func SetDomain(key string, domain string, conn *redis.Client) error {
return conn.Cmd("HSET", "$domainkeys", key, domain).Err
}
78 changes: 78 additions & 0 deletions authkeys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
// "fmt"
"github.com/fzzy/radix/extra/pool"
"github.com/fzzy/radix/redis"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"testing"
)

// Set up the suite for running tests with Redis
type Suite struct {
suite.Suite
}

func (suite *Suite) SetupTest() {
var err error
df := func(network, addr string) (*redis.Client, error) {
client, err := redis.Dial(network, addr)
// fmt.Println("DIaling")
if err != nil {
return nil, err
}
err = client.Cmd("SELECT", 8).Err
if err != nil {
return nil, err
}
err = client.Cmd("FLUSHDB").Err
if err != nil {
return nil, err
}
return client, nil
}
redisPool, err = pool.NewCustomPool("tcp", redisURL, 1, df)
if err != nil {
panic(err)
}
}

func TestSuite(t *testing.T) {
suite.Run(t, new(Suite))
}

func (suite *Suite) TestGetDomainSomething() {
// authKeys = new(AuthKeys)
// authKeys.Init()

c, err := redisPool.Get()
errHndlr(err)
defer redisPool.Put(c)
err = c.Cmd("HSET", "$domainkeys", "xyz1234567890", "peterbe.com").Err
errHndlr(err)

domain, err := GetDomain("xyz1234567890", c)
errHndlr(err)
assert.Equal(
suite.T(),
domain,
"peterbe.com",
)
}

func (suite *Suite) TestGetDomainNothing() {
// authKeys = new(AuthKeys)
// authKeys.Init()

c, err := redisPool.Get()
errHndlr(err)

domain, err := GetDomain("xyz1234567890", c)
errHndlr(err)
assert.Equal(
suite.T(),
domain,
"",
)
}
31 changes: 13 additions & 18 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,12 @@ func updateHandler(w http.ResponseWriter, req *http.Request) {
form.URL = strings.Trim(form.URL, " ")
group := form.Group

// if len(form.Group) != 0 {
// groups = append(groups, strings.Split(form.Groups, ",")...)
// }
// sort.Strings(groups)

c, err := redisPool.Get()
errHndlr(err)
defer redisPool.Put(c)

domain, err := authKeys.GetDomain(key, c)
if err != nil {
domain, err := GetDomain(key, c)
if domain == "" {
output := map[string]string{"error": "Auth-Key not recognized"}
renderer.JSON(w, http.StatusForbidden, output)
return
Expand Down Expand Up @@ -240,8 +235,8 @@ func deleteHandler(w http.ResponseWriter, req *http.Request) {
errHndlr(err)
defer redisPool.CarefullyPut(c, &err)

domain, err := authKeys.GetDomain(key, c)
if err != nil {
domain, err := GetDomain(key, c)
if domain == "" {
output := map[string]string{"error": "Auth-Key not recognized"}
renderer.JSON(w, http.StatusForbidden, output)
return
Expand Down Expand Up @@ -458,7 +453,7 @@ func privateStatsHandler(w http.ResponseWriter, req *http.Request) {
errHndlr(err)
defer redisPool.Put(c)

domain, err := authKeys.GetDomain(key, c)
domain, err := GetDomain(key, c)
if err != nil {
output := map[string]string{"error": "Auth-Key not recognized"}
renderer.JSON(w, http.StatusForbidden, output)
Expand Down Expand Up @@ -504,12 +499,12 @@ func privateStatsHandler(w http.ResponseWriter, req *http.Request) {
}

var (
redisPool *pool.Pool
procs int
debug = true
renderer = render.New()
redisURL = "127.0.0.1:6379"
authKeys *AuthKeys
redisPool *pool.Pool
procs int
debug = true
renderer = render.New()
redisURL = "127.0.0.1:6379"
// authKeys *AuthKeys
staticPrefix = ""
)

Expand Down Expand Up @@ -577,8 +572,8 @@ func main() {
redisPool, err = pool.NewCustomPool("tcp", redisURL, redisPoolSize, df)
errHndlr(err)

authKeys = new(AuthKeys)
authKeys.Init()
// authKeys = new(AuthKeys)
// authKeys.Init()

mux := mux.NewRouter()
mux.HandleFunc("/", indexHandler).Methods("GET", "HEAD")
Expand Down
9 changes: 2 additions & 7 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"encoding/json"
"fmt"
// "fmt"
"github.com/fzzy/radix/extra/pool"
"github.com/fzzy/radix/redis"
"github.com/stretchr/testify/assert"
Expand All @@ -20,9 +20,7 @@ type HandlerSuite struct {
}

func (suite *HandlerSuite) SetupTest() {
fmt.Println("In SetupTest()")
var err error
// var redis_url =
df := func(network, addr string) (*redis.Client, error) {
client, err := redis.Dial(network, addr)
// fmt.Println("DIaling")
Expand All @@ -43,14 +41,11 @@ func (suite *HandlerSuite) SetupTest() {
if err != nil {
panic(err)
}
authKeys = new(AuthKeys)
authKeys.Init()

c, err := redisPool.Get()
errHndlr(err)
defer redisPool.Put(c)
err = c.Cmd("HSET", "$domainkeys", "xyz1234567890", "peterbe.com").Err
errHndlr(err)
SetDomain("xyz1234567890", "peterbe.com", c)

}

Expand Down

0 comments on commit 2101ce4

Please sign in to comment.