gobuffalo / buffalo-auth

Buffalo auth plugin helps adding username password authentication to your app
https://gobuffalo.io
MIT License
41 stars 28 forks source link

user_test.go Tests Fail | undefined: ModelSuite #38

Closed zeknox closed 4 years ago

zeknox commented 5 years ago

Hi! I’ve installed the buffalo-auth plugin using the command go getgithub.com/gobuffalo/buffalo-auth. This completes properly and the buffalo-auth command runs properly from my $PATH.

I've run the buffalo-auth auth command against my project and this has generated proper auth structure; however, when running the default tests which are generated its throwing multiple errors:

$GOPATH/src/coke/models/user_test.go:7:11: undefined: ModelSuite
$GOPATH/src/coke/models/user_test.go:29:11: undefined: ModelSuite
$GOPATH/src/coke/models/user_test.go:48:11: undefined: ModelSuite

Looking at the default code which is generated, the test functions are passing in the argument (ms *ModelSuite) but this is not defined in a location that the test is aware of.

I've been able to resolve this issue by modifying the default code to include the ModelSuite struct. Original Code generated by buffalo-auth:

package models_test

import (
    "coke/models"
)

func (ms *ModelSuite) Test_User_Create() {
    count, err := ms.DB.Count("users")
    ms.NoError(err)
    ms.Equal(0, count)

    u := &models.User{
        Email:                "mark@example.com",
        Password:             "password",
        PasswordConfirmation: "password",
    }
    ms.Zero(u.PasswordHash)

    verrs, err := u.Create(ms.DB)
    ms.NoError(err)
    ms.False(verrs.HasAny())
    ms.NotZero(u.PasswordHash)

    count, err = ms.DB.Count("users")
    ms.NoError(err)
    ms.Equal(1, count)
}

[... SNIP ...]

Fixed code which addresses the undefined condition and allows tests to run properly and pass. The primary additions are defining the struct and adding the additional import line.

package models_test

import (
    "coke/models"

    "github.com/gobuffalo/suite"
)

type ModelSuite struct {
    *suite.Model
}

func (ms *ModelSuite) Test_User_Create() {
    count, err := ms.DB.Count("users")
    ms.NoError(err)
    ms.Equal(0, count)

    u := &models.User{
        Email:                "mark@example.com",
        Password:             "password",
        PasswordConfirmation: "password",
    }
    ms.Zero(u.PasswordHash)

    verrs, err := u.Create(ms.DB)
    ms.NoError(err)
    ms.False(verrs.HasAny())
    ms.NotZero(u.PasswordHash)

    count, err = ms.DB.Count("users")
    ms.NoError(err)
    ms.Equal(1, count)
}

[... SNIP ...]

I'm curious if there is something I've done wrong in the way I've installed the plugin or generated the auth code. Is this something the buffalo-auth auth generator should add to the user_test.go file by default? Any insight on the proper work flow or testing process would be greatly appreciated.

cmorbitzer commented 4 years ago

I can confirm this issue exists when models_test.go, where ModelSuite is defined, belongs to the models package instead of the models_test package. This seems to be the default behavior for apps created with buffalo new. This is fixed by changing package models in models_test.go to package models_test.

paganotoni commented 4 years ago

This one was covered in v1.0.8. please give it a shot and keep us posted.

cmorbitzer commented 4 years ago

@paganotoni That fixed it. Thanks!