Commit 14430bf5 authored by Julien Schröter's avatar Julien Schröter Committed by Simon Metzler

Make resource endpoint receive resource id instead of uri

parent 5d0caa15
Pipeline #1623 passed with stages
in 15 minutes and 38 seconds
......@@ -353,7 +353,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/internal-error'
'/resource/{uri}':
'/resource/{resource_id}':
get:
summary: Get resources such as images and files.
description: >-
......@@ -361,14 +361,16 @@ paths:
This endpoint is a proxy to a hetzner storage box. See the hetzner storage box http API documentation for further information https://wiki.hetzner.de/index.php/Storage_Boxes/en.
parameters:
- name: uri
description: The UniqueresourceIdentifier of the resource
description: The ID of the resource
in: path
required: true
schema:
type: string
type: integer
responses:
'200':
description: The uri was found and the resource is returned. See https://wiki.hetzner.de/index.php/Storage_Boxes/en for more info.
description: The resource was found and the resource is returned. See https://wiki.hetzner.de/index.php/Storage_Boxes/en for more info.
'404':
description: Failed to find resource with id in the database.
'/duel':
get:
......
......@@ -3,14 +3,16 @@ package resource
import (
"net/http"
"net/http/httputil"
"strings"
"strconv"
"github.com/getsentry/raven-go"
"github.com/gin-gonic/gin"
"gitlab.akamu.de/akamu/game-server-go/config"
)
func SetupResourceRoutes(group *gin.RouterGroup, resourceServer *config.ResourceServer) {
func SetupResourceRoutes(group *gin.RouterGroup, repository ResourceQuery, resourceServer *config.ResourceServer) {
group.OPTIONS("", func(ctx *gin.Context) {
ctx.Header("Allow", "GET, POST, PUT")
ctx.Status(200)
......@@ -22,30 +24,48 @@ func SetupResourceRoutes(group *gin.RouterGroup, resourceServer *config.Resource
rs = *resourceServer
}
group.GET("/:uri", getResource(rs))
group.GET("/:rid", getResource(repository, rs))
}
/*
* Redirects the given request to the server, that stores resources.
* The value of the query parameter "uri" will be used as RequestURI.
*/
func ResourceServerDirector(resourceServer config.ResourceServer) func(req *http.Request) {
func ResourceServerDirector(resourceServer config.ResourceServer, resource string) func(req *http.Request) {
return func(req *http.Request) {
req.URL.Scheme = "https"
req.URL.Host = resourceServer.Host
req.Host = resourceServer.Host
req.SetBasicAuth(resourceServer.User, resourceServer.Password)
uri := strings.Replace(req.URL.Path, "/resource/", "", 1)
req.URL.RawQuery = ""
req.URL.Path = "/" + uri
req.RequestURI = "/" + uri
req.URL.Path = "/" + resource
req.RequestURI = "/" + resource
}
}
func getResource(resourceServer config.ResourceServer) gin.HandlerFunc {
func getResource(repository ResourceQuery, resourceServer config.ResourceServer) gin.HandlerFunc {
return func(ctx *gin.Context) {
director := ResourceServerDirector(resourceServer)
resourceIDString := ctx.Param("rid")
resourceID, err := strconv.Atoi(resourceIDString)
if err != nil {
ctx.String(http.StatusBadRequest, "received non-numeric resource id")
return
}
resource, err := repository.Select(uint32(resourceID))
if err != nil {
if err == ErrResourceNotFound {
ctx.AbortWithStatus(http.StatusNotFound)
} else {
raven.CaptureError(err, nil)
ctx.AbortWithError(http.StatusInternalServerError, err)
}
return
}
director := ResourceServerDirector(resourceServer, resource)
proxy := &httputil.ReverseProxy{Director: director}
proxy.ServeHTTP(ctx.Writer, ctx.Request)
......
package resource
import (
"database/sql"
"errors"
"fmt"
"gitlab.akamu.de/akamu/game-server-go/dbhandler"
)
var ErrResourceNotFound error = errors.New("resource not found")
type ResourceQuery interface {
Select(resourceID uint32) (string, error)
}
type MySQLResourceQuery struct{}
func (MySQLResourceQuery) Select(resourceID uint32) (string, error) {
db, err := dbhandler.GetDBConnection()
if err != nil {
return "", fmt.Errorf("failed to receive database connection: %v", err)
}
row := db.QueryRow("SELECT `uri` FROM `resource` WHERE `idresource`=?", resourceID)
var uri string
if err := row.Scan(&uri); err != nil {
if err == sql.ErrNoRows {
return "", ErrResourceNotFound
}
return "", fmt.Errorf("failed to fetch uri: %v", err)
}
return uri, nil
}
......@@ -10,6 +10,8 @@ import (
"strings"
"time"
"gitlab.akamu.de/akamu/game-server-go/endpoint/resource"
"gitlab.akamu.de/akamu/game-server-go/endpoint/refreshjwt"
"github.com/getsentry/raven-go"
......@@ -206,7 +208,7 @@ func main() {
panic("Setting could not be loaded." + errLoadSettings.Error())
}
router := setup.SetupRoutes(&course.MySQLTitleQuery{}, &flashcard.MySQLFlashcardQuery{}, &subject.MySQLSubjectQuery{}, &title.MySQLTitleQuery{}, &traininglist.MySQLTrainingListQuery{}, &user.MySQLUserQuery{}, &pool.MySQLPoolQuery{}, &friend.MySQLFriendQuery{}, &avatar.MySQLAvatarQuery{}, &duel.MySQLDuelQuery{}, &refreshjwt.MySQLRefreshJWTQuery{}, &cfg.ResourceServer, settings, &cfg.SMTP, cfg.JwtSecret)
router := setup.SetupRoutes(&course.MySQLTitleQuery{}, &flashcard.MySQLFlashcardQuery{}, &subject.MySQLSubjectQuery{}, &title.MySQLTitleQuery{}, &traininglist.MySQLTrainingListQuery{}, &user.MySQLUserQuery{}, &pool.MySQLPoolQuery{}, &friend.MySQLFriendQuery{}, &avatar.MySQLAvatarQuery{}, &duel.MySQLDuelQuery{}, &refreshjwt.MySQLRefreshJWTQuery{}, &resource.MySQLResourceQuery{}, &cfg.ResourceServer, settings, &cfg.SMTP, cfg.JwtSecret)
raven.CapturePanic(func() {
// By default it serves on :8080 unless a
......
......@@ -116,7 +116,7 @@ func getRoot(ctx *gin.Context) {
ctx.String(http.StatusOK, "AKAMU REST API")
}
func SetupRoutes(courseRepository course.CourseQuery, flashcardRepository flashcard.FlashcardQuery, subjectRepository subject.SubjectQuery, titleRepository title.TitleQuery, traininglistRepository traininglist.TrainingListQuery, userRepository user.UserQuery, poolRepository pool.PoolQuery, friendRepository friend.FriendQuery, avatarRepository avatar.AvatarQuery, duelRepository duel.DuelQuery, refreshjwtRepository refreshjwt.RefreshJWTQuery, resourceServerConfig *config.ResourceServer, settings *setting.Settings, smtpTransport *sendmail.Transport, jwtSecret ...string) *gin.Engine {
func SetupRoutes(courseRepository course.CourseQuery, flashcardRepository flashcard.FlashcardQuery, subjectRepository subject.SubjectQuery, titleRepository title.TitleQuery, traininglistRepository traininglist.TrainingListQuery, userRepository user.UserQuery, poolRepository pool.PoolQuery, friendRepository friend.FriendQuery, avatarRepository avatar.AvatarQuery, duelRepository duel.DuelQuery, refreshjwtRepository refreshjwt.RefreshJWTQuery, resourceRepository resource.ResourceQuery, resourceServerConfig *config.ResourceServer, settings *setting.Settings, smtpTransport *sendmail.Transport, jwtSecret ...string) *gin.Engine {
//gin.SetMode(gin.ReleaseMode)
router := gin.Default()
......@@ -250,7 +250,7 @@ func SetupRoutes(courseRepository course.CourseQuery, flashcardRepository flashc
friend.SetupFriendRoutes(authGroup.Group("/friend"), friendRepository)
avatar.SetupAvatarRoutes(authGroup.Group("/avatar"), avatarRepository)
duel.SetupDuelRoutes(authGroup.Group("/duel"), duelRepository, titleRepository, refreshjwtRepository)
resource.SetupResourceRoutes(authGroup.Group("/resource"), resourceServerConfig)
resource.SetupResourceRoutes(authGroup.Group("/resource"), resourceRepository, resourceServerConfig)
return router
}
......@@ -15,7 +15,7 @@ import (
func Test_isAvailable_200_true(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -38,7 +38,7 @@ func Test_isAvailable_200_true(t *testing.T) {
func Test_isAvailable_200_false(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, &mockTitle{}, nil, &mockUser{}, nil, nil, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, &mockTitle{}, nil, &mockUser{}, nil, nil, nil, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......
......@@ -29,7 +29,7 @@ func (mockAvatar) SelectAll() ([]schemas.AvatarSchema, error) {
type mockAvatarError struct{}
func Test_getAvatars200(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, &mockAvatar{}, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, &mockAvatar{}, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -45,7 +45,7 @@ func Test_getAvatars200(t *testing.T) {
}
func Test_getAvatars401(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, &mockAvatar{}, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, &mockAvatar{}, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -58,7 +58,7 @@ func Test_getAvatars401(t *testing.T) {
}
func Test_getAllAvatars200(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, &mockAvatar{}, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, &mockAvatar{}, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -74,7 +74,7 @@ func Test_getAllAvatars200(t *testing.T) {
}
func Test_getAllAvatars401(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, &mockAvatar{}, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, &mockAvatar{}, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......
......@@ -301,7 +301,7 @@ func newMockDuel() mockDuel {
func Test_getAllDuels200(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
// Test with user1
env1 := new(TestEnv)
......@@ -336,7 +336,7 @@ func Test_getAllDuels200(t *testing.T) {
func Test_getDuel200(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -355,7 +355,7 @@ func Test_getDuel200(t *testing.T) {
func Test_getDuel401(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -370,7 +370,7 @@ func Test_getDuel401(t *testing.T) {
func Test_getDuel403(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -386,7 +386,7 @@ func Test_getDuel403(t *testing.T) {
func Test_getDuel404(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -401,7 +401,7 @@ func Test_getDuel404(t *testing.T) {
func Test_postDuel201(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -423,7 +423,7 @@ func Test_postDuel201(t *testing.T) {
func Test_postDuel400(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
backup, _ := json.Marshal(duelRepo.duels)
......@@ -453,7 +453,7 @@ func Test_postDuel400(t *testing.T) {
func Test_postDuel401(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -467,7 +467,7 @@ func Test_postDuel401(t *testing.T) {
func Test_postDuel403(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -482,7 +482,7 @@ func Test_postDuel403(t *testing.T) {
func Test_postDuel404(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -497,7 +497,7 @@ func Test_postDuel404(t *testing.T) {
func Test_getDuelPool200(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -515,7 +515,7 @@ func Test_getDuelPool200(t *testing.T) {
func Test_getDuelPool400(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -530,7 +530,7 @@ func Test_getDuelPool400(t *testing.T) {
func Test_getDuelPool401(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -544,7 +544,7 @@ func Test_getDuelPool401(t *testing.T) {
func Test_getDuelPool403(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -559,7 +559,7 @@ func Test_getDuelPool403(t *testing.T) {
func Test_getDuelPool404(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -574,7 +574,7 @@ func Test_getDuelPool404(t *testing.T) {
func Test_patchDuelPool200(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -608,7 +608,7 @@ func Test_patchDuelPool200(t *testing.T) {
func Test_patchDuelPool400(t *testing.T) {
duelRepo := newMockDuel()
expect, _ := json.Marshal(duelRepo.duels)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -632,7 +632,7 @@ func Test_patchDuelPool400(t *testing.T) {
func Test_patchDuelPool401(t *testing.T) {
duelRepo := newMockDuel()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -647,7 +647,7 @@ func Test_patchDuelPool401(t *testing.T) {
func Test_patchDuelPool403(t *testing.T) {
duelRepo := newMockDuel()
expected, _ := json.Marshal(duelRepo.duels)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -673,7 +673,7 @@ func Test_patchDuelPool403(t *testing.T) {
func Test_patchDuelPool404(t *testing.T) {
duelRepo := newMockDuel()
expected, _ := json.Marshal(duelRepo.duels)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, &duelRepo, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......
......@@ -12,6 +12,8 @@ import (
"path"
"testing"
"gitlab.akamu.de/akamu/game-server-go/endpoint/resource"
"github.com/gin-gonic/gin"
"github.com/kinbiko/jsonassert"
"github.com/stretchr/testify/assert"
......@@ -95,7 +97,7 @@ func (env *E2EEnvironment) SetupEnvironment() (*gin.Engine, error) {
return nil, errSettings
}
router := setup.SetupRoutes(&course.MySQLTitleQuery{}, &flashcard.MySQLFlashcardQuery{}, &subject.MySQLSubjectQuery{}, &title.MySQLTitleQuery{}, &traininglist.MySQLTrainingListQuery{}, &user.MySQLUserQuery{}, &pool.MySQLPoolQuery{}, &friend.MySQLFriendQuery{}, &avatar.MySQLAvatarQuery{}, &duel.MySQLDuelQuery{}, &refreshjwt.MySQLRefreshJWTQuery{}, &env.Config.ResourceServer, &settings, nil, env.Config.JwtSecret)
router := setup.SetupRoutes(&course.MySQLTitleQuery{}, &flashcard.MySQLFlashcardQuery{}, &subject.MySQLSubjectQuery{}, &title.MySQLTitleQuery{}, &traininglist.MySQLTrainingListQuery{}, &user.MySQLUserQuery{}, &pool.MySQLPoolQuery{}, &friend.MySQLFriendQuery{}, &avatar.MySQLAvatarQuery{}, &duel.MySQLDuelQuery{}, &refreshjwt.MySQLRefreshJWTQuery{}, &resource.MySQLResourceQuery{}, &env.Config.ResourceServer, &settings, nil, env.Config.JwtSecret)
return router, err
}
......
......@@ -77,7 +77,7 @@ func (m *mockFriend) Delete(userID, userIDToRemove uint32) ([]schemas.InfoUserSc
func Test_getFriend200(t *testing.T) {
friendRepo := &mockFriend{friends: make(map[uint32][]schemas.InfoUserSchema)}
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil, nil)
expected := []schemas.InfoUserSchema{getMockUserInfo(mockUsers[2]), getMockUserInfo(mockUsers[3])}
......@@ -101,7 +101,7 @@ func Test_getFriend200(t *testing.T) {
func Test_getFriend401(t *testing.T) {
friendRepo := &mockFriend{friends: make(map[uint32][]schemas.InfoUserSchema)}
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
res := httptest.NewRecorder()
......@@ -114,7 +114,7 @@ func Test_getFriend401(t *testing.T) {
func Test_postFriend200(t *testing.T) {
friendRepo := &mockFriend{friends: make(map[uint32][]schemas.InfoUserSchema)}
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil, nil)
expected := []schemas.InfoUserSchema{getMockUserInfo(mockUsers[3])}
......@@ -152,7 +152,7 @@ func Test_postFriend200(t *testing.T) {
func Test_postFriend401(t *testing.T) {
friendRepo := &mockFriend{friends: make(map[uint32][]schemas.InfoUserSchema)}
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
res := httptest.NewRecorder()
......@@ -165,7 +165,7 @@ func Test_postFriend401(t *testing.T) {
func Test_postFriend400(t *testing.T) {
friendRepo := &mockFriend{friends: make(map[uint32][]schemas.InfoUserSchema)}
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil, nil)
expected := []schemas.InfoUserSchema{getMockUserInfo(mockUsers[3])}
......@@ -192,7 +192,7 @@ func Test_postFriend400(t *testing.T) {
func Test_postFriend404(t *testing.T) {
friendRepo := &mockFriend{friends: make(map[uint32][]schemas.InfoUserSchema)}
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil, nil)
expected := []schemas.InfoUserSchema{getMockUserInfo(mockUsers[3])}
......@@ -219,7 +219,7 @@ func Test_postFriend404(t *testing.T) {
func Test_postFriendTwice400(t *testing.T) {
friendRepo := &mockFriend{friends: make(map[uint32][]schemas.InfoUserSchema)}
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil, nil)
expected := []schemas.InfoUserSchema{getMockUserInfo(mockUsers[3])}
......@@ -246,7 +246,7 @@ func Test_postFriendTwice400(t *testing.T) {
func Test_deleteFriend200(t *testing.T) {
friendRepo := &mockFriend{friends: make(map[uint32][]schemas.InfoUserSchema)}
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil, nil)
expected := []schemas.InfoUserSchema{getMockUserInfo(mockUsers[3])}
......@@ -286,7 +286,7 @@ func Test_deleteFriend200(t *testing.T) {
func Test_deleteFriend401(t *testing.T) {
friendRepo := &mockFriend{friends: make(map[uint32][]schemas.InfoUserSchema)}
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
res := httptest.NewRecorder()
......@@ -299,7 +299,7 @@ func Test_deleteFriend401(t *testing.T) {
func Test_deleteFriend404(t *testing.T) {
friendRepo := &mockFriend{friends: make(map[uint32][]schemas.InfoUserSchema)}
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, friendRepo, nil, nil, newSession(), nil, nil, nil, nil)
friendRepo.friends[1] = []schemas.InfoUserSchema{getMockUserInfo(mockUsers[2])}
......
......@@ -27,7 +27,7 @@ func (p *mockPool) PatchUserPools(userID uint32, selection []uint32) error {
}
func Test_getPools(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, &mockPool{}, nil, nil, nil, newSession(), nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, &mockPool{}, nil, nil, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......
......@@ -58,7 +58,7 @@ func (mockSession) GetLatestToken(userID uint32) (string, error) {
func Test_patchRefreshJWT200(t *testing.T) {
repository := newSession()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, nil, repository, nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, nil, repository, nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -91,7 +91,7 @@ func Test_patchRefreshJWT200(t *testing.T) {
func Test_patchRefreshJWT401(t *testing.T) {
// No matching session found
repository := newSession()
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, nil, repository, nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, nil, nil, &mockUser{}, nil, nil, nil, nil, repository, nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......@@ -115,7 +115,7 @@ func Test_patchRefreshJWT401(t *testing.T) {
func Test_JWT440(t *testing.T) {
repository := newSession()
router := setup.SetupRoutes(nil, nil, nil, &mockTitle{}, nil, &mockUser{}, nil, nil, nil, nil, repository, nil, nil, nil)
router := setup.SetupRoutes(nil, nil, nil, &mockTitle{}, nil, &mockUser{}, nil, nil, nil, nil, repository, nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
......
......@@ -15,12 +15,22 @@ import (
var resourceServer = config.ResourceServer{Host: "testhost", User: "testuser", Password: "testpassword"}
type mockResource struct{}
func (mockResource) Select(resourceID uint32) (string, error) {
if resourceID == 1 {
return "testfile", nil
}
return "", resource.ErrResourceNotFound
}
func Test_ResourceServerDirector(t *testing.T) {
t.Skip("resource server seems not to work")
wantedResURI := "testURI"
wantedResURI := "testfile"
req, _ := http.NewRequest("GET", "/resource?uri="+wantedResURI, nil)
resource.ResourceServerDirector(resourceServer)(req)
req, _ := http.NewRequest("GET", "/resource/1", nil)
resource.ResourceServerDirector(resourceServer, wantedResURI)(req)
assert.Equal(t, req.URL.Host, resourceServer.Host)
assert.Equal(t, req.URL.Scheme, "https")
......@@ -29,19 +39,44 @@ func Test_ResourceServerDirector(t *testing.T) {
assert.Equal(t, req.Header.Get("Authorization"), "Basic "+base64.StdEncoding.EncodeToString([]byte(resourceServer.User+":"+resourceServer.Password)))
}