forked from fabric8-services/fabric8-tenant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect if the user to init on Tenant already has a project on OSO (OS…
…IO#1446) Include option in User service to support custom HTTP transport Rename vars in controller.Tenant to avoid collision with packages (`user` and `cluster`) Add test on the controller.TenantController.Setup() method using go-vcr Fixes openshiftio/openshift.io#1446 Signed-off-by: Xavier Coulon <[email protected]>
- Loading branch information
Showing
8 changed files
with
276 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package controller_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
jwt "github.com/dgrijalva/jwt-go" | ||
"github.com/fabric8-services/fabric8-tenant/app/test" | ||
"github.com/fabric8-services/fabric8-tenant/cluster" | ||
"github.com/fabric8-services/fabric8-tenant/configuration" | ||
"github.com/fabric8-services/fabric8-tenant/controller" | ||
"github.com/fabric8-services/fabric8-tenant/openshift" | ||
"github.com/fabric8-services/fabric8-tenant/tenant" | ||
testsupport "github.com/fabric8-services/fabric8-tenant/test" | ||
"github.com/fabric8-services/fabric8-tenant/test/gormsupport" | ||
"github.com/fabric8-services/fabric8-tenant/test/recorder" | ||
"github.com/fabric8-services/fabric8-tenant/token" | ||
"github.com/fabric8-services/fabric8-tenant/user" | ||
"github.com/fabric8-services/fabric8-wit/resource" | ||
"github.com/goadesign/goa" | ||
goajwt "github.com/goadesign/goa/middleware/security/jwt" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type TenantControllerTestSuite struct { | ||
gormsupport.DBTestSuite | ||
} | ||
|
||
func TestTenantController(t *testing.T) { | ||
resource.Require(t, resource.Database) | ||
suite.Run(t, &TenantControllerTestSuite{DBTestSuite: gormsupport.NewDBTestSuite("../config.yaml")}) | ||
} | ||
|
||
func (s *TenantControllerTestSuite) TestInitTenant() { | ||
// given | ||
r, err := recorder.New("../test/data/controller/setup_tenant", recorder.WithJWTMatcher()) | ||
require.NoError(s.T(), err) | ||
defer r.Stop() | ||
authURL := "http://authservice" | ||
resolveToken := token.NewResolve(authURL, configuration.WithRoundTripper(r.Transport)) | ||
// tok, err := testsupport.NewToken("tenant_service", "../test/private_key.pem") | ||
// require.NoError(s.T(), err) | ||
svc := goa.New("Tenants-service") | ||
saToken, err := testsupport.NewToken("tenant_service", "../test/private_key.pem") | ||
require.NoError(s.T(), err) | ||
clusterService := cluster.NewService( | ||
authURL, | ||
saToken.Raw, | ||
resolveToken, | ||
token.NewGPGDecypter("foo"), | ||
configuration.WithRoundTripper(r.Transport), | ||
) | ||
clusters, err := clusterService.GetClusters(context.Background()) | ||
require.NoError(s.T(), err) | ||
resolveCluster := cluster.NewResolve(clusters) | ||
resolveTenant := func(ctx context.Context, target, userToken string) (user, accessToken string, err error) { | ||
// log.Debug(ctx, map[string]interface{}{"user_token": userToken}, "attempting to resolve tenant for user...") | ||
return resolveToken(ctx, target, userToken, false, token.PlainText) // no need to use "forcePull=true" to validate the user's token on the target. | ||
} | ||
tenantService := tenant.NewDBService(s.DB) | ||
userService := user.NewService( | ||
authURL, | ||
saToken.Raw, | ||
configuration.WithRoundTripper(r.Transport), | ||
) | ||
defaultOpenshiftConfig := openshift.Config{} | ||
templateVars := make(map[string]string) | ||
ctrl := controller.NewTenantController(svc, tenantService, userService, resolveTenant, resolveCluster, defaultOpenshiftConfig, templateVars) | ||
require.NotNil(s.T(), ctrl) | ||
|
||
s.T().Run("accepted", func(t *testing.T) { | ||
// given | ||
tenantID := "83fdcae2-634f-4a52-958a-f723cb621700" | ||
ctx, err := createValidUserContext(tenantID, "[email protected]") | ||
require.NoError(t, err) | ||
// when/then | ||
test.SetupTenantAccepted(t, ctx, svc, ctrl) | ||
}) | ||
|
||
s.T().Run("fail", func(t *testing.T) { | ||
|
||
t.Run("user already exists", func(t *testing.T) { | ||
// given | ||
tenantID := "83fdcae2-634f-4a52-958a-f723cb621700" | ||
ctx, err := createValidUserContext(tenantID, "[email protected]") | ||
require.NoError(t, err) | ||
// when/then | ||
test.SetupTenantAccepted(t, ctx, svc, ctrl) | ||
}) | ||
|
||
t.Run("missing token", func(t *testing.T) { | ||
// given | ||
test.SetupTenantUnauthorized(t, context.Background(), svc, ctrl) | ||
}) | ||
}) | ||
|
||
} | ||
|
||
func createValidUserContext(userID, email string) (context.Context, error) { | ||
claims := jwt.MapClaims{} | ||
var token *jwt.Token = nil | ||
if userID != "" { | ||
claims["sub"] = userID | ||
claims["email"] = email | ||
token = jwt.NewWithClaims(jwt.SigningMethodRS512, claims) | ||
// use the test private key to sign the token | ||
key, err := testsupport.PrivateKey("../test/private_key.pem") | ||
if err != nil { | ||
return nil, err | ||
} | ||
signed, err := token.SignedString(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
token.Raw = signed | ||
} | ||
return goajwt.WithJWT(context.Background(), token), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.