Added better testing with gocheck

Fixed some typos leading to syntax errors
TODO: Finish up gorm integration into templates
pull/24/head
Jordan 2014-03-26 21:42:07 -05:00
parent 776f591f68
commit cb9c405f46
6 changed files with 43 additions and 22 deletions

View File

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
) )
type SMTPServer struct { type SMTPServer struct {
@ -27,7 +26,6 @@ func init() {
config_file, err := ioutil.ReadFile("./config.json") config_file, err := ioutil.ReadFile("./config.json")
if err != nil { if err != nil {
fmt.Printf("File error: %v\n", err) fmt.Printf("File error: %v\n", err)
os.Exit(1)
} }
json.Unmarshal(config_file, &Conf) json.Unmarshal(config_file, &Conf)
} }

View File

@ -38,7 +38,7 @@ import (
func main() { func main() {
//Setup the global variables and settings //Setup the global variables and settings
err := models.Setup() err := models.Setup()
defer models.DB.Close() //defer models.db.DB().Close()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }

View File

@ -1,14 +0,0 @@
package main
import (
"testing"
"github.com/jordan-wright/gophish/models"
)
func TestDBSetup(t *testing.T) {
err := models.Setup()
if err != nil {
t.Fatalf("Failed creating database: %v", err)
}
}

View File

@ -33,7 +33,7 @@ func GetCampaigns(uid int64) ([]Campaign, error) {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
*/for i, _ := range cs { for i, _ := range cs {
err := db.Model(&cs[i]).Related(&cs[i].Results).Error err := db.Model(&cs[i]).Related(&cs[i].Results).Error
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -47,11 +47,11 @@ func GetCampaigns(uid int64) ([]Campaign, error) {
func GetCampaign(id int64, uid int64) (Campaign, error) { func GetCampaign(id int64, uid int64) (Campaign, error) {
c := Campaign{} c := Campaign{}
err := db.Where("id = ?", id).Where("user_id = ?", uid).Find(&c).Error err := db.Where("id = ?", id).Where("user_id = ?", uid).Find(&c).Error
*/if err != nil { if err != nil {
return c, err return c, err
} }
err = db.Model(&c).Related(&c.Results).Error err = db.Model(&c).Related(&c.Results).Error
*/return c, err return c, err
} }
// PostCampaign inserts a campaign and all associated records into the database. // PostCampaign inserts a campaign and all associated records into the database.

View File

@ -1,7 +1,6 @@
package models package models
import ( import (
"database/sql"
"errors" "errors"
"log" "log"
"os" "os"
@ -14,7 +13,6 @@ import (
var Conn *gorp.DbMap var Conn *gorp.DbMap
var db gorm.DB var db gorm.DB
var DB *sql.DB
var err error var err error
var ErrUsernameTaken = errors.New("username already taken") var ErrUsernameTaken = errors.New("username already taken")
var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile) var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile)
@ -29,6 +27,7 @@ type Flash struct {
// It also populates the Gophish Config object // It also populates the Gophish Config object
func Setup() error { func Setup() error {
db, err = gorm.Open("sqlite3", config.Conf.DBPath) db, err = gorm.Open("sqlite3", config.Conf.DBPath)
db.LogMode(true)
db.SetLogger(Logger) db.SetLogger(Logger)
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)

38
models/models_test.go Normal file
View File

@ -0,0 +1,38 @@
package models
import (
"os"
"testing"
"github.com/jordan-wright/gophish/config"
"launchpad.net/gocheck"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { gocheck.TestingT(t) }
type ModelsSuite struct{}
var _ = gocheck.Suite(&ModelsSuite{})
func (s *ModelsSuite) SetUpSuite(c *gocheck.C) {
config.Conf.DBPath = "../gophish_test.db"
err := Setup()
if err != nil {
c.Fatalf("Failed creating database: %v", err)
}
}
func (s *ModelsSuite) TestGetUser(c *gocheck.C) {
u, err := GetUser(1)
c.Assert(err, gocheck.Equals, nil)
c.Assert(u.Username, gocheck.Equals, "admin")
}
func (s *ModelsSuite) TearDownSuite(c *gocheck.C) {
db.DB().Close()
err := os.Remove(config.Conf.DBPath)
if err != nil {
c.Fatalf("Failed deleting test database: %v", err)
}
}