2021-03-22 22:26:54 +01:00
|
|
|
/*
|
|
|
|
GoToSocial
|
|
|
|
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-05-08 14:25:55 +02:00
|
|
|
package auth_test
|
2021-03-22 22:26:54 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2021-04-01 20:46:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-05-15 11:58:11 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db/pg"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-04-01 20:46:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
2021-03-22 22:26:54 +01:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2021-04-01 20:46:45 +02:00
|
|
|
type AuthTestSuite struct {
|
2021-03-22 22:26:54 +01:00
|
|
|
suite.Suite
|
2021-04-01 20:46:45 +02:00
|
|
|
oauthServer oauth.Server
|
2021-03-22 22:26:54 +01:00
|
|
|
db db.DB
|
2021-04-19 19:42:19 +02:00
|
|
|
testAccount *gtsmodel.Account
|
|
|
|
testApplication *gtsmodel.Application
|
|
|
|
testUser *gtsmodel.User
|
2021-04-01 20:46:45 +02:00
|
|
|
testClient *oauth.Client
|
2021-03-22 22:26:54 +01:00
|
|
|
config *config.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupSuite sets some variables on the suite that we can use as consts (more or less) throughout
|
2021-04-01 20:46:45 +02:00
|
|
|
func (suite *AuthTestSuite) SetupSuite() {
|
2021-03-22 22:26:54 +01:00
|
|
|
c := config.Empty()
|
|
|
|
// we're running on localhost without https so set the protocol to http
|
|
|
|
c.Protocol = "http"
|
|
|
|
// just for testing
|
|
|
|
c.Host = "localhost:8080"
|
|
|
|
// because go tests are run within the test package directory, we need to fiddle with the templateconfig
|
|
|
|
// basedir in a way that we wouldn't normally have to do when running the binary, in order to make
|
|
|
|
// the templates actually load
|
|
|
|
c.TemplateConfig.BaseDir = "../../../web/template/"
|
|
|
|
c.DBConfig = &config.DBConfig{
|
|
|
|
Type: "postgres",
|
|
|
|
Address: "localhost",
|
|
|
|
Port: 5432,
|
|
|
|
User: "postgres",
|
|
|
|
Password: "postgres",
|
|
|
|
Database: "postgres",
|
|
|
|
ApplicationName: "gotosocial",
|
|
|
|
}
|
|
|
|
suite.config = c
|
|
|
|
|
|
|
|
encryptedPassword, err := bcrypt.GenerateFromPassword([]byte("password"), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Panicf("error encrypting user pass: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
acctID := uuid.NewString()
|
|
|
|
|
2021-04-19 19:42:19 +02:00
|
|
|
suite.testAccount = >smodel.Account{
|
2021-03-22 22:26:54 +01:00
|
|
|
ID: acctID,
|
|
|
|
Username: "test_user",
|
|
|
|
}
|
2021-04-19 19:42:19 +02:00
|
|
|
suite.testUser = >smodel.User{
|
2021-03-22 22:26:54 +01:00
|
|
|
EncryptedPassword: string(encryptedPassword),
|
|
|
|
Email: "user@example.org",
|
|
|
|
AccountID: acctID,
|
|
|
|
}
|
2021-04-01 20:46:45 +02:00
|
|
|
suite.testClient = &oauth.Client{
|
2021-03-22 22:26:54 +01:00
|
|
|
ID: "a-known-client-id",
|
|
|
|
Secret: "some-secret",
|
|
|
|
Domain: fmt.Sprintf("%s://%s", c.Protocol, c.Host),
|
|
|
|
}
|
2021-04-19 19:42:19 +02:00
|
|
|
suite.testApplication = >smodel.Application{
|
2021-03-22 22:26:54 +01:00
|
|
|
Name: "a test application",
|
|
|
|
Website: "https://some-application-website.com",
|
|
|
|
RedirectURI: "http://localhost:8080",
|
|
|
|
ClientID: "a-known-client-id",
|
|
|
|
ClientSecret: "some-secret",
|
|
|
|
Scopes: "read",
|
|
|
|
VapidKey: uuid.NewString(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupTest creates a postgres connection and creates the oauth_clients table before each test
|
2021-04-01 20:46:45 +02:00
|
|
|
func (suite *AuthTestSuite) SetupTest() {
|
2021-03-22 22:26:54 +01:00
|
|
|
|
|
|
|
log := logrus.New()
|
|
|
|
log.SetLevel(logrus.TraceLevel)
|
2021-05-15 11:58:11 +02:00
|
|
|
db, err := pg.NewPostgresService(context.Background(), suite.config, log)
|
2021-03-22 22:26:54 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Panicf("error creating database connection: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.db = db
|
|
|
|
|
|
|
|
models := []interface{}{
|
2021-04-01 20:46:45 +02:00
|
|
|
&oauth.Client{},
|
|
|
|
&oauth.Token{},
|
2021-04-19 19:42:19 +02:00
|
|
|
>smodel.User{},
|
|
|
|
>smodel.Account{},
|
|
|
|
>smodel.Application{},
|
2021-03-22 22:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range models {
|
|
|
|
if err := suite.db.CreateTable(m); err != nil {
|
|
|
|
logrus.Panicf("db connection error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 20:46:45 +02:00
|
|
|
suite.oauthServer = oauth.New(suite.db, log)
|
2021-03-22 22:26:54 +01:00
|
|
|
|
|
|
|
if err := suite.db.Put(suite.testAccount); err != nil {
|
|
|
|
logrus.Panicf("could not insert test account into db: %s", err)
|
|
|
|
}
|
|
|
|
if err := suite.db.Put(suite.testUser); err != nil {
|
|
|
|
logrus.Panicf("could not insert test user into db: %s", err)
|
|
|
|
}
|
|
|
|
if err := suite.db.Put(suite.testClient); err != nil {
|
|
|
|
logrus.Panicf("could not insert test client into db: %s", err)
|
|
|
|
}
|
|
|
|
if err := suite.db.Put(suite.testApplication); err != nil {
|
|
|
|
logrus.Panicf("could not insert test application into db: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TearDownTest drops the oauth_clients table and closes the pg connection after each test
|
2021-04-01 20:46:45 +02:00
|
|
|
func (suite *AuthTestSuite) TearDownTest() {
|
2021-03-22 22:26:54 +01:00
|
|
|
models := []interface{}{
|
2021-04-01 20:46:45 +02:00
|
|
|
&oauth.Client{},
|
|
|
|
&oauth.Token{},
|
2021-04-19 19:42:19 +02:00
|
|
|
>smodel.User{},
|
|
|
|
>smodel.Account{},
|
|
|
|
>smodel.Application{},
|
2021-03-22 22:26:54 +01:00
|
|
|
}
|
|
|
|
for _, m := range models {
|
|
|
|
if err := suite.db.DropTable(m); err != nil {
|
|
|
|
logrus.Panicf("error dropping table: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := suite.db.Stop(context.Background()); err != nil {
|
|
|
|
logrus.Panicf("error closing db connection: %s", err)
|
|
|
|
}
|
|
|
|
suite.db = nil
|
|
|
|
}
|
|
|
|
|
2021-04-01 20:46:45 +02:00
|
|
|
func TestAuthTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(AuthTestSuite))
|
2021-03-22 22:26:54 +01:00
|
|
|
}
|