1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-03-23 21:50:50 +02:00

Increase test coverage in authboss.

This commit is contained in:
Aaron 2015-02-24 13:03:06 -08:00
parent faa35b66be
commit e369d626a8
5 changed files with 136 additions and 53 deletions

27
client_storer_test.go Normal file
View File

@ -0,0 +1,27 @@
package authboss
import "testing"
type testClientStorerErr string
func (t testClientStorerErr) Put(key, value string) {}
func (t testClientStorerErr) Get(key string) (string, bool) {
return string(t), key == string(t)
}
func (t testClientStorerErr) Del(key string) {}
func TestClientStorerErr(t *testing.T) {
var cs testClientStorerErr
csw := clientStoreWrapper{&cs}
if _, err := csw.GetErr("hello"); err == nil {
t.Error("Expected an error")
}
cs = "hello"
if str, err := csw.GetErr("hello"); err != nil {
t.Error(err)
} else if str != "hello" {
t.Error("Wrong value:", str)
}
}

View File

@ -34,6 +34,38 @@ func TestContext_Request(t *testing.T) {
if post, ok := ctx.FirstPostFormValue("post"); !ok || post != "form" {
t.Error("Postform value not getting recorded correctly.")
}
if _, err := ctx.FirstFormValueErr("query"); err != nil {
t.Error(err)
}
if _, err := ctx.FirstPostFormValueErr("post"); err != nil {
t.Error(err)
}
if query, ok := ctx.FormValue("query1"); ok {
t.Error("Expected query1 not to be found:", query)
}
if post, ok := ctx.PostFormValue("post1"); ok {
t.Error("Expected post1 not to be found:", post)
}
if query, ok := ctx.FirstFormValue("query1"); ok {
t.Error("Expected query1 not to be found:", query)
}
if post, ok := ctx.FirstPostFormValue("post1"); ok {
t.Error("Expected post1 not to be found:", post)
}
if query, err := ctx.FirstFormValueErr("query1"); err == nil {
t.Error("Expected query1 not to be found:", query)
}
if post, err := ctx.FirstPostFormValueErr("post1"); err == nil {
t.Error("Expected post1 not to be found:", post)
}
}
func TestContext_SaveUser(t *testing.T) {

View File

@ -49,3 +49,18 @@ func TestMailer(t *testing.T) {
t.Error("Html body not present.")
}
}
func TestSMTPMailer(t *testing.T) {
var _ Mailer = SMTPMailer("server", nil)
recovered := false
defer func() {
recovered = recover() != nil
}()
SMTPMailer("", nil)
if !recovered {
t.Error("Should have panicd.")
}
}

View File

@ -309,56 +309,3 @@ func underToCamel(in string) string {
}
return out.String()
}
/*type postgresStorer struct {
*sql.DB
}
type postgresUser struct {
// Anything Else
CustomAttribute string
// AuthBoss attributes.
Email string
}
func (p *postgresStorer) Put(key string, attr Attributes) error {
u := postgresUser{}
if err := attr.Bind(&u); err != nil {
panic("I should have written my user struct better!")
}
_, err := p.Exec("update users set email = $1 where id = $2", u.Email, key)
if err != nil {
return err
}
return nil
}
func (p *postgresStorer) Create(key string, attr Attributes) error {
u := postgresUser{
CustomAttribute: "DefaultValue",
}
if err := attr.Bind(&u); err != nil {
panic("I should have written my user struct better!")
}
_, err := p.Exec("insert into users (custom_attribute, email) values ($1)", u.CustomAttribute, u.Email)
if err != nil {
return err
}
return nil
}
func (p *postgresStorer) Get(key string, attrMeta AttributeMeta) (interface{}, error) {
row := p.QueryRow(`select * from users where key = $1`, key)
u := postgresUser{}
if err := row.Scan(&u.CustomAttribute, &u.Email); err != nil {
return nil, err
}
return u, nil
}*/

View File

@ -52,6 +52,68 @@ func TestAttributeMeta_Names(t *testing.T) {
}
}
func TestAttributeMeta_Helpers(t *testing.T) {
now := time.Now()
attr := Attributes{
"integer": 5,
"string": "a",
"bool": true,
"date_time": now,
}
if str, ok := attr.String("string"); !ok || str != "a" {
t.Error(str, ok)
}
if str, err := attr.StringErr("string"); err != nil || str != "a" {
t.Error(str, err)
}
if str, ok := attr.String("notstring"); ok {
t.Error(str, ok)
}
if str, err := attr.StringErr("notstring"); err == nil {
t.Error(str, err)
}
if integer, ok := attr.Int("integer"); !ok || integer != 5 {
t.Error(integer, ok)
}
if integer, err := attr.IntErr("integer"); err != nil || integer != 5 {
t.Error(integer, err)
}
if integer, ok := attr.Int("notinteger"); ok {
t.Error(integer, ok)
}
if integer, err := attr.IntErr("notinteger"); err == nil {
t.Error(integer, err)
}
if boolean, ok := attr.Bool("bool"); !ok || !boolean {
t.Error(boolean, ok)
}
if boolean, err := attr.BoolErr("bool"); err != nil || !boolean {
t.Error(boolean, err)
}
if boolean, ok := attr.Bool("notbool"); ok {
t.Error(boolean, ok)
}
if boolean, err := attr.BoolErr("notbool"); err == nil {
t.Error(boolean, err)
}
if date, ok := attr.DateTime("date_time"); !ok || date != now {
t.Error(date, ok)
}
if date, err := attr.DateTimeErr("date_time"); err != nil || date != now {
t.Error(date, err)
}
if date, ok := attr.DateTime("notdate_time"); ok {
t.Error(date, ok)
}
if date, err := attr.DateTimeErr("notdate_time"); err == nil {
t.Error(date, err)
}
}
func TestDataType_String(t *testing.T) {
t.Parallel()