Skip to content

Commit

Permalink
Add email tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dimensi0n committed Mar 24, 2020
1 parent b925f18 commit 6c80df5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/dimensi0n/persona

go 1.14

require (
github.com/jinzhu/gorm v1.9.1
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
Expand Down
84 changes: 81 additions & 3 deletions persona_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
_ "github.com/jinzhu/gorm/dialects/sqlite"
)

func TestSignup(t *testing.T) {
func TestSignupWithUsername(t *testing.T) {

w := httptest.NewRecorder()

Expand Down Expand Up @@ -39,7 +39,7 @@ func TestSignup(t *testing.T) {
}
}

func TestLogout(t *testing.T) {
func TestLogoutWithUsername(t *testing.T) {
w := httptest.NewRecorder()

db, err := gorm.Open("sqlite3", "test.db")
Expand All @@ -63,7 +63,60 @@ func TestLogout(t *testing.T) {
}
}

func TestLogin(t *testing.T) {
func TestSignupWithEmail(t *testing.T) {

w := httptest.NewRecorder()

db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
t.Error(err)
}
defer db.Close()

db.DropTable(&User{})
db.DropTable(&Sessionusername{})

db.AutoMigrate(&User{})

Config(db, "email")

user := User{Username: "Erwan", Password: HashPassword("0000"), Mail: "[email protected]"}
err = Signup(&user, user.Mail, w)
if err != nil {
t.Error(err)
}

resp := w.Result()
if resp.Cookies()[0].Value == "" {
t.Error(errors.New("no session cookie"))
}
}

func TestLogoutWithEmail(t *testing.T) {
w := httptest.NewRecorder()

db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
t.Error(err)
}
defer db.Close()

Config(db, "email")

user := User{Username: "Erwan", Password: "0000", Mail: "[email protected]"}

err = Logout(user.Mail, w)
if err != nil {
t.Error(err)
}

resp := w.Result()
if resp.Cookies()[0].Value != "" {
t.Error(errors.New("cookie was not deleted"))
}
}

func TestLoginWithUsername(t *testing.T) {

w := httptest.NewRecorder()

Expand All @@ -88,6 +141,31 @@ func TestLogin(t *testing.T) {
}
}

func TestLoginWithEmail(t *testing.T) {

w := httptest.NewRecorder()

db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
t.Error(err)
}
defer db.Close()

Config(db, "email")

user := User{Username: "Erwan", Password: "0000", Mail: "[email protected]"}

err = Login(user.Mail, user.Password, w)
if err != nil {
t.Error(err)
}

resp := w.Result()
if resp.Cookies()[0].Value == "" {
t.Error(errors.New("no session cookie"))
}
}

func TestCurrentUser(t *testing.T) {
w := httptest.NewRecorder()

Expand Down

0 comments on commit 6c80df5

Please sign in to comment.