github.com/cs3org/reva/v2@v2.27.7/pkg/auth/manager/nextcloud/nextcloud_test.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package nextcloud_test
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"os"
    25  
    26  	"google.golang.org/grpc/metadata"
    27  
    28  	authpb "github.com/cs3org/go-cs3apis/cs3/auth/provider/v1beta1"
    29  	userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    30  	types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
    31  
    32  	"github.com/cs3org/reva/v2/pkg/auth/manager/nextcloud"
    33  	"github.com/cs3org/reva/v2/pkg/auth/scope"
    34  	ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
    35  	jwt "github.com/cs3org/reva/v2/pkg/token/manager/jwt"
    36  
    37  	. "github.com/onsi/ginkgo/v2"
    38  	. "github.com/onsi/gomega"
    39  )
    40  
    41  func setUpNextcloudServer() (*nextcloud.Manager, *[]string, func()) {
    42  	var conf *nextcloud.AuthManagerConfig
    43  
    44  	ncHost := os.Getenv("NEXTCLOUD")
    45  	if len(ncHost) == 0 {
    46  		conf = &nextcloud.AuthManagerConfig{
    47  			EndPoint: "http://mock.com/apps/sciencemesh/",
    48  			MockHTTP: true,
    49  		}
    50  		nc, _ := nextcloud.NewAuthManager(conf)
    51  		called := make([]string, 0)
    52  		h := nextcloud.GetNextcloudServerMock(&called)
    53  		mock, teardown := nextcloud.TestingHTTPClient(h)
    54  		nc.SetHTTPClient(mock)
    55  		return nc, &called, teardown
    56  	}
    57  	conf = &nextcloud.AuthManagerConfig{
    58  		EndPoint: ncHost + "/apps/sciencemesh/",
    59  		MockHTTP: false,
    60  	}
    61  	nc, _ := nextcloud.NewAuthManager(conf)
    62  	return nc, nil, func() {}
    63  }
    64  
    65  func checkCalled(called *[]string, expected string) {
    66  	if called == nil {
    67  		return
    68  	}
    69  	Expect(len(*called)).To(Equal(1))
    70  	Expect((*called)[0]).To(Equal(expected))
    71  }
    72  
    73  var _ = Describe("Nextcloud", func() {
    74  	var (
    75  		ctx     context.Context
    76  		options map[string]interface{}
    77  		tmpRoot string
    78  		user    = &userpb.User{
    79  			Id: &userpb.UserId{
    80  				Idp:      "0.0.0.0:19000",
    81  				OpaqueId: "f7fbf8c8-139b-4376-b307-cf0a8c2d0d9c",
    82  				Type:     userpb.UserType_USER_TYPE_PRIMARY,
    83  			},
    84  			Username: "tester",
    85  		}
    86  	)
    87  
    88  	BeforeEach(func() {
    89  		var err error
    90  
    91  		options = map[string]interface{}{
    92  			"endpoint":  "http://mock.com/apps/sciencemesh/",
    93  			"mock_http": true,
    94  		}
    95  
    96  		ctx = context.Background()
    97  
    98  		// Add auth token
    99  		tokenManager, err := jwt.New(map[string]interface{}{"secret": "changemeplease"})
   100  		Expect(err).ToNot(HaveOccurred())
   101  		scope, err := scope.AddOwnerScope(nil)
   102  		Expect(err).ToNot(HaveOccurred())
   103  		t, err := tokenManager.MintToken(ctx, user, scope)
   104  		Expect(err).ToNot(HaveOccurred())
   105  		ctx = ctxpkg.ContextSetToken(ctx, t)
   106  		ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, t)
   107  		ctx = ctxpkg.ContextSetUser(ctx, user)
   108  	})
   109  
   110  	AfterEach(func() {
   111  		if tmpRoot != "" {
   112  			os.RemoveAll(tmpRoot)
   113  		}
   114  	})
   115  
   116  	Describe("New", func() {
   117  		It("returns a new instance", func() {
   118  			fmt.Println(options)
   119  			_, err := nextcloud.New(options)
   120  			Expect(err).ToNot(HaveOccurred())
   121  		})
   122  	})
   123  
   124  	// Authenticate(ctx context.Context, clientID, clientSecret string) (*user.User, map[string]*authpb.Scope, error)
   125  	Describe("Authenticate", func() {
   126  		It("calls the GetHome endpoint", func() {
   127  			am, called, teardown := setUpNextcloudServer()
   128  			defer teardown()
   129  
   130  			user, scope, err := am.Authenticate(ctx, "einstein", "relativity")
   131  			Expect(err).ToNot(HaveOccurred())
   132  			Expect(user).To(Equal(&userpb.User{
   133  				Id: &userpb.UserId{
   134  					Idp:      "some-idp",
   135  					OpaqueId: "some-opaque-user-id",
   136  					Type:     1,
   137  				},
   138  				Username:     "",
   139  				Mail:         "",
   140  				MailVerified: false,
   141  				DisplayName:  "",
   142  				Groups:       nil,
   143  				Opaque:       nil,
   144  				UidNumber:    0,
   145  				GidNumber:    0,
   146  			}))
   147  			Expect(scope).To(Equal(map[string]*authpb.Scope{
   148  				"user": {
   149  					Resource: &types.OpaqueEntry{
   150  						Decoder: "json",
   151  						Value:   []byte("{\"resource_id\":{\"storage_id\":\"storage-id\",\"opaque_id\":\"opaque-id\"},\"path\":\"some/file/path.txt\"}"),
   152  					},
   153  					Role: 1,
   154  				},
   155  			}))
   156  			checkCalled(called, `POST /apps/sciencemesh/~einstein/api/auth/Authenticate {"clientID":"einstein","clientSecret":"relativity"}`)
   157  		})
   158  	})
   159  
   160  })