How to use Release method of protect Package

Best Selenoid code snippet using protect.Release

operations_test.go

Source:operations_test.go Github

copy

Full Screen

...182 rr := handleRequest(t, op, "/v1/policy/containment-policy", http.MethodPut, bytes.NewReader(body))183 require.Equal(t, http.StatusInternalServerError, rr.Code)184 })185}186func TestReleaseHandler(t *testing.T) {187 req := operation.ReleaseRequest{188 DID: targetDID,189 }190 t.Run("Success", func(t *testing.T) {191 ctrl := gomock.NewController(t)192 releaseService := NewMockReleaseService(ctrl)193 releaseService.EXPECT().Release(gomock.Any(), targetDID).Return(&ticket.Ticket{}, nil).Times(1)194 protectService := NewMockProtectService(ctrl)195 protectService.EXPECT().Get(gomock.Any(), targetDID).196 Return(&protect.ProtectedData{PolicyID: testPolicyID}, nil).Times(1)197 policyService := NewMockPolicyService(ctrl)198 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Handler).Return(nil).Times(1)199 subjectResolver := NewMockSubjectResolver(ctrl)200 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil)201 op := &operation.Operation{202 ReleaseService: releaseService,203 PolicyService: policyService,204 ProtectService: protectService,205 SubjectResolver: subjectResolver,206 }207 body, err := json.Marshal(req)208 require.NoError(t, err)209 rr := handleRequest(t, op, "/v1/release", http.MethodPost, bytes.NewReader(body))210 require.Equal(t, http.StatusOK, rr.Code)211 })212 t.Run("Fail to unmarshal request body", func(t *testing.T) {213 ctrl := gomock.NewController(t)214 svc := NewMockReleaseService(ctrl)215 svc.EXPECT().Release(gomock.Any(), targetDID).Times(0)216 op := &operation.Operation{217 ReleaseService: svc,218 }219 rr := handleRequest(t, op, "/v1/release", http.MethodPost, bytes.NewBufferString("invalid json"))220 require.Equal(t, http.StatusBadRequest, rr.Code)221 })222 t.Run("Fail to get protected data", func(t *testing.T) {223 ctrl := gomock.NewController(t)224 releaseService := NewMockReleaseService(ctrl)225 releaseService.EXPECT().Release(gomock.Any(), gomock.Any()).Times(0)226 protectService := NewMockProtectService(ctrl)227 protectService.EXPECT().Get(gomock.Any(), targetDID).Return(nil, errors.New("get error")).Times(1)228 policyService := NewMockPolicyService(ctrl)229 policyService.EXPECT().Check(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(0)230 subjectResolver := NewMockSubjectResolver(ctrl)231 subjectResolver.EXPECT().Resolve(gomock.Any()).Times(0)232 op := &operation.Operation{233 ReleaseService: releaseService,234 PolicyService: policyService,235 ProtectService: protectService,236 SubjectResolver: subjectResolver,237 }238 body, err := json.Marshal(req)239 require.NoError(t, err)240 rr := handleRequest(t, op, "/v1/release", http.MethodPost, bytes.NewReader(body))241 require.Equal(t, http.StatusInternalServerError, rr.Code)242 })243 t.Run("Fail to check policy: ErrNotAllowed", func(t *testing.T) {244 ctrl := gomock.NewController(t)245 releaseService := NewMockReleaseService(ctrl)246 releaseService.EXPECT().Release(gomock.Any(), gomock.Any()).Times(0)247 protectService := NewMockProtectService(ctrl)248 protectService.EXPECT().Get(gomock.Any(), targetDID).249 Return(&protect.ProtectedData{PolicyID: testPolicyID}, nil).Times(1)250 policyService := NewMockPolicyService(ctrl)251 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Handler).252 Return(policy.ErrNotAllowed).Times(1)253 subjectResolver := NewMockSubjectResolver(ctrl)254 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil)255 op := &operation.Operation{256 ReleaseService: releaseService,257 PolicyService: policyService,258 ProtectService: protectService,259 SubjectResolver: subjectResolver,260 }261 body, err := json.Marshal(req)262 require.NoError(t, err)263 rr := handleRequest(t, op, "/v1/release", http.MethodPost, bytes.NewReader(body))264 require.Equal(t, http.StatusUnauthorized, rr.Code)265 })266 t.Run("Fail to create release transaction on a DID", func(t *testing.T) {267 ctrl := gomock.NewController(t)268 releaseService := NewMockReleaseService(ctrl)269 releaseService.EXPECT().Release(gomock.Any(), targetDID).Return(nil, errors.New("release error"))270 protectService := NewMockProtectService(ctrl)271 protectService.EXPECT().Get(gomock.Any(), targetDID).272 Return(&protect.ProtectedData{PolicyID: testPolicyID}, nil).Times(1)273 policyService := NewMockPolicyService(ctrl)274 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Handler).Return(nil).Times(1)275 subjectResolver := NewMockSubjectResolver(ctrl)276 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil)277 op := &operation.Operation{278 ReleaseService: releaseService,279 PolicyService: policyService,280 ProtectService: protectService,281 SubjectResolver: subjectResolver,282 }283 body, err := json.Marshal(req)284 require.NoError(t, err)285 rr := handleRequest(t, op, "/v1/release", http.MethodPost, bytes.NewReader(body))286 require.Equal(t, http.StatusInternalServerError, rr.Code)287 })288}289func TestAuthorizeHandler(t *testing.T) {290 t.Run("Success", func(t *testing.T) {291 ctrl := gomock.NewController(t)292 releaseService := NewMockReleaseService(ctrl)293 releaseService.EXPECT().Get(gomock.Any(), testTicketID).Return(&ticket.Ticket{294 ID: testTicketID,295 DID: targetDID,296 Status: 0,297 }, nil)298 releaseService.EXPECT().Authorize(gomock.Any(), testTicketID, subjectDID).Return(nil)299 protectService := NewMockProtectService(ctrl)300 protectService.EXPECT().Get(gomock.Any(), targetDID).Return(&protect.ProtectedData{301 PolicyID: testPolicyID,302 }, nil)303 policyService := NewMockPolicyService(ctrl)304 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Approver).Return(nil)305 subjectResolver := NewMockSubjectResolver(ctrl)306 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil)307 op := &operation.Operation{308 ReleaseService: releaseService,309 PolicyService: policyService,310 ProtectService: protectService,311 SubjectResolver: subjectResolver,312 }313 rr := handleRequest(t, op, "/v1/release/test-ticket/authorize", http.MethodPost, nil)314 require.Equal(t, http.StatusOK, rr.Code)315 })316 t.Run("Ticket not found", func(t *testing.T) {317 ctrl := gomock.NewController(t)318 releaseService := NewMockReleaseService(ctrl)319 releaseService.EXPECT().Get(gomock.Any(), testTicketID).Return(nil, storage.ErrDataNotFound)320 op := &operation.Operation{321 ReleaseService: releaseService,322 }323 rr := handleRequest(t, op, "/v1/release/test-ticket/authorize", http.MethodPost, nil)324 require.Equal(t, http.StatusBadRequest, rr.Code)325 })326 t.Run("Fail to get ticket", func(t *testing.T) {327 ctrl := gomock.NewController(t)328 releaseService := NewMockReleaseService(ctrl)329 releaseService.EXPECT().Get(gomock.Any(), testTicketID).Return(nil, errors.New("get error"))330 op := &operation.Operation{331 ReleaseService: releaseService,332 }333 rr := handleRequest(t, op, "/v1/release/test-ticket/authorize", http.MethodPost, nil)334 require.Equal(t, http.StatusInternalServerError, rr.Code)335 })336 t.Run("Fail to get protected data", func(t *testing.T) {337 ctrl := gomock.NewController(t)338 releaseService := NewMockReleaseService(ctrl)339 releaseService.EXPECT().Get(gomock.Any(), testTicketID).Return(&ticket.Ticket{340 ID: testTicketID,341 DID: targetDID,342 Status: 0,343 }, nil)344 protectService := NewMockProtectService(ctrl)345 protectService.EXPECT().Get(gomock.Any(), targetDID).Return(nil, errors.New("get error"))346 op := &operation.Operation{347 ReleaseService: releaseService,348 ProtectService: protectService,349 }350 rr := handleRequest(t, op, "/v1/release/test-ticket/authorize", http.MethodPost, nil)351 require.Equal(t, http.StatusInternalServerError, rr.Code)352 })353 t.Run("Not allowed per policy", func(t *testing.T) {354 ctrl := gomock.NewController(t)355 releaseService := NewMockReleaseService(ctrl)356 releaseService.EXPECT().Get(gomock.Any(), testTicketID).Return(&ticket.Ticket{357 ID: testTicketID,358 DID: targetDID,359 Status: 0,360 }, nil)361 protectService := NewMockProtectService(ctrl)362 protectService.EXPECT().Get(gomock.Any(), targetDID).Return(&protect.ProtectedData{363 PolicyID: testPolicyID,364 }, nil)365 policyService := NewMockPolicyService(ctrl)366 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Approver).Return(policy.ErrNotAllowed)367 subjectResolver := NewMockSubjectResolver(ctrl)368 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil)369 op := &operation.Operation{370 ReleaseService: releaseService,371 PolicyService: policyService,372 ProtectService: protectService,373 SubjectResolver: subjectResolver,374 }375 rr := handleRequest(t, op, "/v1/release/test-ticket/authorize", http.MethodPost, nil)376 require.Equal(t, http.StatusUnauthorized, rr.Code)377 })378 t.Run("Fail to authorize ticket", func(t *testing.T) {379 ctrl := gomock.NewController(t)380 releaseService := NewMockReleaseService(ctrl)381 releaseService.EXPECT().Get(gomock.Any(), testTicketID).Return(&ticket.Ticket{382 ID: testTicketID,383 DID: targetDID,384 Status: 0,385 }, nil)386 releaseService.EXPECT().Authorize(gomock.Any(), testTicketID, subjectDID).Return(errors.New("authorize error"))387 protectService := NewMockProtectService(ctrl)388 protectService.EXPECT().Get(gomock.Any(), targetDID).Return(&protect.ProtectedData{389 PolicyID: testPolicyID,390 }, nil)391 policyService := NewMockPolicyService(ctrl)392 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Approver).Return(nil)393 subjectResolver := NewMockSubjectResolver(ctrl)394 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil)395 op := &operation.Operation{396 ReleaseService: releaseService,397 PolicyService: policyService,398 ProtectService: protectService,399 SubjectResolver: subjectResolver,400 }401 rr := handleRequest(t, op, "/v1/release/test-ticket/authorize", http.MethodPost, nil)402 require.Equal(t, http.StatusInternalServerError, rr.Code)403 })404}405func TestTicketStatusHandler(t *testing.T) {406 t.Run("Success", func(t *testing.T) {407 ctrl := gomock.NewController(t)408 releaseService := NewMockReleaseService(ctrl)409 releaseService.EXPECT().Get(gomock.Any(), testTicketID).Return(&ticket.Ticket{410 ID: testTicketID,411 DID: targetDID,412 Status: 0,413 }, nil)414 protectService := NewMockProtectService(ctrl)415 protectService.EXPECT().Get(gomock.Any(), targetDID).Return(&protect.ProtectedData{416 PolicyID: testPolicyID,417 }, nil)418 policyService := NewMockPolicyService(ctrl)419 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Handler).Return(nil)420 subjectResolver := NewMockSubjectResolver(ctrl)421 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil)422 op := &operation.Operation{423 ReleaseService: releaseService,424 PolicyService: policyService,425 ProtectService: protectService,426 SubjectResolver: subjectResolver,427 }428 rr := handleRequest(t, op, "/v1/release/test-ticket/status", http.MethodGet, nil)429 require.Equal(t, http.StatusOK, rr.Code)430 })431 t.Run("Ticket not found", func(t *testing.T) {432 ctrl := gomock.NewController(t)433 releaseService := NewMockReleaseService(ctrl)434 releaseService.EXPECT().Get(gomock.Any(), testTicketID).Return(nil, storage.ErrDataNotFound)435 op := &operation.Operation{436 ReleaseService: releaseService,437 }438 rr := handleRequest(t, op, "/v1/release/test-ticket/status", http.MethodGet, nil)439 require.Equal(t, http.StatusBadRequest, rr.Code)440 })441 t.Run("Fail to get ticket", func(t *testing.T) {442 ctrl := gomock.NewController(t)443 releaseService := NewMockReleaseService(ctrl)444 releaseService.EXPECT().Get(gomock.Any(), testTicketID).Return(nil, errors.New("get error"))445 op := &operation.Operation{446 ReleaseService: releaseService,447 }448 rr := handleRequest(t, op, "/v1/release/test-ticket/status", http.MethodGet, nil)449 require.Equal(t, http.StatusInternalServerError, rr.Code)450 })451 t.Run("Fail to get protected data", func(t *testing.T) {452 ctrl := gomock.NewController(t)453 releaseService := NewMockReleaseService(ctrl)454 releaseService.EXPECT().Get(gomock.Any(), testTicketID).Return(&ticket.Ticket{455 ID: testTicketID,456 DID: targetDID,457 Status: 0,458 }, nil)459 protectService := NewMockProtectService(ctrl)460 protectService.EXPECT().Get(gomock.Any(), targetDID).Return(nil, errors.New("get error"))461 op := &operation.Operation{462 ReleaseService: releaseService,463 ProtectService: protectService,464 }465 rr := handleRequest(t, op, "/v1/release/test-ticket/status", http.MethodGet, nil)466 require.Equal(t, http.StatusInternalServerError, rr.Code)467 })468 t.Run("Not allowed per policy", func(t *testing.T) {469 ctrl := gomock.NewController(t)470 releaseService := NewMockReleaseService(ctrl)471 releaseService.EXPECT().Get(gomock.Any(), testTicketID).Return(&ticket.Ticket{472 ID: testTicketID,473 DID: targetDID,474 Status: 0,475 }, nil)476 protectService := NewMockProtectService(ctrl)477 protectService.EXPECT().Get(gomock.Any(), targetDID).Return(&protect.ProtectedData{478 PolicyID: testPolicyID,479 }, nil)480 policyService := NewMockPolicyService(ctrl)481 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Handler).Return(policy.ErrNotAllowed)482 subjectResolver := NewMockSubjectResolver(ctrl)483 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil)484 op := &operation.Operation{485 ReleaseService: releaseService,486 PolicyService: policyService,487 ProtectService: protectService,488 SubjectResolver: subjectResolver,489 }490 rr := handleRequest(t, op, "/v1/release/test-ticket/status", http.MethodGet, nil)491 require.Equal(t, http.StatusUnauthorized, rr.Code)492 })493}494func TestCollectHandler(t *testing.T) {495 const (496 testDID = "did:example:test"497 testPolicyID = "test-policy"498 testTicketID = "ticket1234"499 testQueryID = "queryID1234"500 )501 protectedData := &protect.ProtectedData{PolicyID: testPolicyID}502 t.Run("Success", func(t *testing.T) {503 ctrl := gomock.NewController(t)504 defer ctrl.Finish()505 releaseService := NewMockReleaseService(ctrl)506 releaseService.EXPECT().Get(gomock.Any(), testTicketID).507 Return(&ticket.Ticket{DID: testDID, Status: ticket.ReadyToCollect}, nil)508 collectService := NewMockCollectService(ctrl)509 collectService.EXPECT().Collect(gomock.Any(), protectedData, subjectDID).Return(testQueryID, nil)510 protectService := NewMockProtectService(ctrl)511 protectService.EXPECT().Get(gomock.Any(), testDID).512 Return(protectedData, nil)513 policyService := NewMockPolicyService(ctrl)514 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Handler).Return(nil)515 subjectResolver := NewMockSubjectResolver(ctrl)516 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil).AnyTimes()517 op := &operation.Operation{518 ReleaseService: releaseService,519 PolicyService: policyService,520 ProtectService: protectService,521 SubjectResolver: subjectResolver,522 CollectService: collectService,523 }524 rr := handleRequest(t, op, "/v1/release/"+testTicketID+"/collect", http.MethodPost, bytes.NewReader([]byte{}))525 require.Equal(t, http.StatusOK, rr.Code)526 })527 t.Run("Fail to get protected data", func(t *testing.T) {528 ctrl := gomock.NewController(t)529 defer ctrl.Finish()530 releaseService := NewMockReleaseService(ctrl)531 releaseService.EXPECT().Get(gomock.Any(), testTicketID).532 Return(&ticket.Ticket{DID: testDID, Status: ticket.ReadyToCollect}, nil).AnyTimes()533 collectService := NewMockCollectService(ctrl)534 collectService.EXPECT().Collect(gomock.Any(), protectedData, subjectDID).535 Return(testQueryID, nil).AnyTimes()536 protectService := NewMockProtectService(ctrl)537 protectService.EXPECT().Get(gomock.Any(), testDID).Return(nil, errors.New("get error"))538 policyService := NewMockPolicyService(ctrl)539 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Handler).540 Return(nil).AnyTimes()541 subjectResolver := NewMockSubjectResolver(ctrl)542 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil).AnyTimes()543 op := &operation.Operation{544 ReleaseService: releaseService,545 PolicyService: policyService,546 ProtectService: protectService,547 SubjectResolver: subjectResolver,548 CollectService: collectService,549 }550 rr := handleRequest(t, op, "/v1/release/"+testTicketID+"/collect", http.MethodPost, bytes.NewReader([]byte{}))551 require.Equal(t, http.StatusInternalServerError, rr.Code)552 })553 t.Run("Fail to get ticket", func(t *testing.T) {554 ctrl := gomock.NewController(t)555 defer ctrl.Finish()556 releaseService := NewMockReleaseService(ctrl)557 releaseService.EXPECT().Get(gomock.Any(), testTicketID).558 Return(nil, errors.New("get error"))559 collectService := NewMockCollectService(ctrl)560 collectService.EXPECT().Collect(gomock.Any(), protectedData, subjectDID).561 Return(testQueryID, nil).AnyTimes()562 protectService := NewMockProtectService(ctrl)563 protectService.EXPECT().Get(gomock.Any(), testDID).Return(protectedData, nil).AnyTimes()564 policyService := NewMockPolicyService(ctrl)565 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Handler).566 Return(nil).AnyTimes()567 subjectResolver := NewMockSubjectResolver(ctrl)568 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil).AnyTimes()569 op := &operation.Operation{570 ReleaseService: releaseService,571 PolicyService: policyService,572 ProtectService: protectService,573 SubjectResolver: subjectResolver,574 CollectService: collectService,575 }576 rr := handleRequest(t, op, "/v1/release/"+testTicketID+"/collect", http.MethodPost, bytes.NewReader([]byte{}))577 require.Equal(t, http.StatusBadRequest, rr.Code)578 })579 t.Run("Fail to check policy: ErrNotAllowed", func(t *testing.T) {580 ctrl := gomock.NewController(t)581 defer ctrl.Finish()582 releaseService := NewMockReleaseService(ctrl)583 releaseService.EXPECT().Get(gomock.Any(), testTicketID).584 Return(&ticket.Ticket{DID: testDID, Status: ticket.ReadyToCollect}, nil).AnyTimes()585 collectService := NewMockCollectService(ctrl)586 collectService.EXPECT().Collect(gomock.Any(), protectedData, subjectDID).587 Return(testQueryID, nil).AnyTimes()588 protectService := NewMockProtectService(ctrl)589 protectService.EXPECT().Get(gomock.Any(), testDID).Return(protectedData, nil).AnyTimes()590 policyService := NewMockPolicyService(ctrl)591 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Handler).592 Return(policy.ErrNotAllowed)593 subjectResolver := NewMockSubjectResolver(ctrl)594 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil).AnyTimes()595 op := &operation.Operation{596 ReleaseService: releaseService,597 PolicyService: policyService,598 ProtectService: protectService,599 SubjectResolver: subjectResolver,600 CollectService: collectService,601 }602 rr := handleRequest(t, op, "/v1/release/"+testTicketID+"/collect", http.MethodPost, bytes.NewReader([]byte{}))603 require.Equal(t, http.StatusUnauthorized, rr.Code)604 })605 t.Run("Unauthorized to collect data", func(t *testing.T) {606 ctrl := gomock.NewController(t)607 defer ctrl.Finish()608 releaseService := NewMockReleaseService(ctrl)609 releaseService.EXPECT().Get(gomock.Any(), testTicketID).610 Return(&ticket.Ticket{DID: testDID, Status: ticket.New}, nil)611 collectService := NewMockCollectService(ctrl)612 collectService.EXPECT().Collect(gomock.Any(), protectedData, subjectDID).613 Times(0)614 protectService := NewMockProtectService(ctrl)615 protectService.EXPECT().Get(gomock.Any(), testDID).Return(protectedData, nil)616 policyService := NewMockPolicyService(ctrl)617 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Handler).618 Return(nil).AnyTimes()619 subjectResolver := NewMockSubjectResolver(ctrl)620 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil).AnyTimes()621 op := &operation.Operation{622 ReleaseService: releaseService,623 PolicyService: policyService,624 ProtectService: protectService,625 SubjectResolver: subjectResolver,626 CollectService: collectService,627 }628 rr := handleRequest(t, op, "/v1/release/"+testTicketID+"/collect", http.MethodPost, bytes.NewReader([]byte{}))629 require.Equal(t, http.StatusUnauthorized, rr.Code)630 })631 t.Run("Fail to collect data", func(t *testing.T) {632 ctrl := gomock.NewController(t)633 defer ctrl.Finish()634 releaseService := NewMockReleaseService(ctrl)635 releaseService.EXPECT().Get(gomock.Any(), testTicketID).636 Return(&ticket.Ticket{DID: testDID, Status: ticket.ReadyToCollect}, nil)637 collectService := NewMockCollectService(ctrl)638 collectService.EXPECT().Collect(gomock.Any(), protectedData, subjectDID).639 Return("", errors.New("collect failed"))640 protectService := NewMockProtectService(ctrl)641 protectService.EXPECT().Get(gomock.Any(), testDID).Return(protectedData, nil)642 policyService := NewMockPolicyService(ctrl)643 policyService.EXPECT().Check(gomock.Any(), testPolicyID, subjectDID, policy.Handler).644 Return(nil)645 subjectResolver := NewMockSubjectResolver(ctrl)646 subjectResolver.EXPECT().Resolve(gomock.Any()).Return(subjectDID, nil).AnyTimes()647 op := &operation.Operation{648 ReleaseService: releaseService,649 PolicyService: policyService,650 ProtectService: protectService,651 SubjectResolver: subjectResolver,652 CollectService: collectService,653 }654 rr := handleRequest(t, op, "/v1/release/"+testTicketID+"/collect", http.MethodPost, bytes.NewReader([]byte{}))655 require.Equal(t, http.StatusInternalServerError, rr.Code)656 })657}658func TestExtractHandler(t *testing.T) {659 const (660 testQueryID = "queryID1234"661 )662 req := operation.ExtractRequest{...

Full Screen

Full Screen

operations.go

Source:operations.go Github

copy

Full Screen

...3SPDX-License-Identifier: Apache-2.04*/5package operation6//nolint:lll7//go:generate mockgen -destination gomocks_test.go -package operation_test -source=operations.go -mock_names policyService=MockPolicyService,protectService=MockProtectService,releaseService=MockReleaseService,subjectResolver=MockSubjectResolver,collectService=MockCollectService,extractService=MockExtractService8import (9 "context"10 "encoding/json"11 "errors"12 "fmt"13 "net/http"14 "strings"15 "github.com/gorilla/mux"16 "github.com/hyperledger/aries-framework-go/pkg/common/log"17 "github.com/hyperledger/aries-framework-go/spi/storage"18 "github.com/trustbloc/ace/pkg/gatekeeper/policy"19 "github.com/trustbloc/ace/pkg/gatekeeper/protect"20 "github.com/trustbloc/ace/pkg/gatekeeper/release/ticket"21 "github.com/trustbloc/ace/pkg/restapi/handler"22 "github.com/trustbloc/ace/pkg/restapi/model"23)24const (25 policyIDVarName = "policy_id"26 ticketIDVarName = "ticket_id"27 baseV1Path = "/v1"28 protectEndpoint = baseV1Path + "/protect"29 policyEndpoint = baseV1Path + "/policy/{" + policyIDVarName + "}"30 releaseEndpoint = baseV1Path + "/release"31 authorizeEndpoint = releaseEndpoint + "/{" + ticketIDVarName + "}/authorize"32 ticketStatusEndpoint = releaseEndpoint + "/{" + ticketIDVarName + "}/status"33 collectEndpoint = releaseEndpoint + "/{" + ticketIDVarName + "}/collect"34 extractEndpoint = baseV1Path + "/extract"35)36var logger = log.New("gatekeeper")37type policyService interface {38 Save(ctx context.Context, doc *policy.Policy) error39 Check(ctx context.Context, policyID, did string, role policy.Role) error40}41type protectService interface {42 Protect(ctx context.Context, data, policyID string) (*protect.ProtectedData, error)43 Get(ctx context.Context, did string) (*protect.ProtectedData, error)44}45type releaseService interface {46 Release(ctx context.Context, did string) (*ticket.Ticket, error)47 Get(ctx context.Context, ticketID string) (*ticket.Ticket, error)48 Authorize(ctx context.Context, ticketID, approverDID string) error49}50type collectService interface {51 Collect(ctx context.Context, protectedData *protect.ProtectedData, requestingPartyDID string) (string, error)52}53type extractService interface {54 Extract(ctx context.Context, authToken string) (string, error)55}56type subjectResolver interface {57 Resolve(ctx context.Context) (string, error)58}59// Operation defines handlers for Gatekeeper operations.60type Operation struct {61 SubjectResolver subjectResolver62 PolicyService policyService63 ProtectService protectService64 ReleaseService releaseService65 CollectService collectService66 ExtractService extractService67}68// GetRESTHandlers get all controller API handler available for this service.69func (o *Operation) GetRESTHandlers() []handler.Handler {70 return []handler.Handler{71 handler.NewHTTPHandler(policyEndpoint, http.MethodPut, o.createPolicyHandler, handler.WithAuth(handler.AuthToken)),72 handler.NewHTTPHandler(protectEndpoint, http.MethodPost, o.protectHandler, handler.WithAuth(handler.AuthHTTPSig)),73 handler.NewHTTPHandler(releaseEndpoint, http.MethodPost, o.releaseHandler, handler.WithAuth(handler.AuthHTTPSig)),74 handler.NewHTTPHandler(authorizeEndpoint, http.MethodPost, o.authorizeHandler, handler.WithAuth(handler.AuthHTTPSig)),75 handler.NewHTTPHandler(ticketStatusEndpoint, http.MethodGet, o.ticketStatusHandler, handler.WithAuth(handler.AuthHTTPSig)), //nolint:lll76 handler.NewHTTPHandler(collectEndpoint, http.MethodPost, o.collectHandler, handler.WithAuth(handler.AuthHTTPSig)),77 handler.NewHTTPHandler(extractEndpoint, http.MethodPost, o.extractHandler),78 }79}80// createPolicyHandler swagger:route PUT /v1/policy/{policy_id} gatekeeper createPolicyReq81//82// Creates policy configuration for storing and releasing protected data.83//84// Authorization: Bearer token85//86// Responses:87// 200: createPolicyResp88// default: errorResp89func (o *Operation) createPolicyHandler(rw http.ResponseWriter, r *http.Request) {90 var p policy.Policy91 err := json.NewDecoder(r.Body).Decode(&p)92 if err != nil {93 respondError(rw, http.StatusBadRequest, err)94 return95 }96 p.ID = strings.ToLower(mux.Vars(r)[policyIDVarName])97 err = o.PolicyService.Save(r.Context(), &p)98 if err != nil {99 respondError(rw, http.StatusInternalServerError, fmt.Errorf("save policy: %w", err))100 return101 }102 respond(rw, http.StatusOK, nil)103}104// protectHandler swagger:route POST /v1/protect gatekeeper protectReq105//106// Converts a social media handle (or other sensitive string data) into a DID.107//108// Authorization: HTTP Signatures (headers="(request-target) date digest")109//110// Responses:111// 200: protectResp112// default: errorResp113func (o *Operation) protectHandler(rw http.ResponseWriter, r *http.Request) {114 var req ProtectRequest115 err := json.NewDecoder(r.Body).Decode(&req)116 if err != nil {117 respondError(rw, http.StatusBadRequest, err)118 return119 }120 if _, err = o.checkPolicy(r.Context(), req.Policy, policy.Collector); err != nil {121 respondError(rw, err.(*policyError).status, err) //nolint:errorlint,forcetypeassert122 return123 }124 protectedData, err := o.ProtectService.Protect(r.Context(), req.Target, req.Policy)125 if err != nil {126 respondError(rw, http.StatusInternalServerError, err)127 return128 }129 respond(rw, http.StatusOK, &ProtectResponse{DID: protectedData.DID})130}131// releaseHandler swagger:route POST /v1/release gatekeeper releaseReq132//133// Creates a new release transaction (ticket) on a DID.134//135// Authorization: HTTP Signatures (headers="(request-target) date")136//137// Responses:138// 200: releaseResp139// default: errorResp140func (o *Operation) releaseHandler(rw http.ResponseWriter, r *http.Request) {141 var req ReleaseRequest142 err := json.NewDecoder(r.Body).Decode(&req)143 if err != nil {144 respondError(rw, http.StatusBadRequest, err)145 return146 }147 protectedData, err := o.ProtectService.Get(r.Context(), req.DID)148 if err != nil {149 respondError(rw, http.StatusInternalServerError, err)150 return151 }152 if _, err = o.checkPolicy(r.Context(), protectedData.PolicyID, policy.Handler); err != nil {153 respondError(rw, err.(*policyError).status, err) //nolint:errorlint,forcetypeassert154 return155 }156 t, err := o.ReleaseService.Release(r.Context(), req.DID)157 if err != nil {158 respondError(rw, http.StatusInternalServerError, err)159 return160 }161 respond(rw, http.StatusOK, &ReleaseResponse{TicketID: t.ID})162}163// authorizeHandler swagger:route POST /v1/release/{ticket_id}/authorize gatekeeper authorizeReq164//165// Authorizes release transaction (ticket).166//167// Authorization: HTTP Signatures (headers="(request-target) date")168//169// Responses:170// 200: authorizeResp171// default: errorResp172func (o *Operation) authorizeHandler(rw http.ResponseWriter, r *http.Request) {173 ticketID := mux.Vars(r)[ticketIDVarName]174 t, err := o.ReleaseService.Get(r.Context(), ticketID)175 if err != nil {176 if errors.Is(err, storage.ErrDataNotFound) {177 respondError(rw, http.StatusBadRequest, err)178 }179 respondError(rw, http.StatusInternalServerError, err)180 return181 }182 protectedData, err := o.ProtectService.Get(r.Context(), t.DID)183 if err != nil {184 respondError(rw, http.StatusInternalServerError, err)185 return186 }187 var sub string188 if sub, err = o.checkPolicy(r.Context(), protectedData.PolicyID, policy.Approver); err != nil {189 respondError(rw, err.(*policyError).status, err) //nolint:errorlint,forcetypeassert190 return191 }192 if err = o.ReleaseService.Authorize(r.Context(), ticketID, sub); err != nil {193 respondError(rw, http.StatusInternalServerError, err)194 return195 }196 respond(rw, http.StatusOK, nil)197}198// ticketStatusHandler swagger:route GET /v1/release/{ticket_id}/status gatekeeper ticketStatusReq199//200// Gets the status of the ticket.201//202// Authorization: HTTP Signatures (headers="(request-target) date")203//204// Responses:205// 200: ticketStatusResp206// default: errorResp207func (o *Operation) ticketStatusHandler(rw http.ResponseWriter, r *http.Request) {208 ticketID := mux.Vars(r)[ticketIDVarName]209 t, err := o.ReleaseService.Get(r.Context(), ticketID)210 if err != nil {211 if errors.Is(err, storage.ErrDataNotFound) {212 respondError(rw, http.StatusBadRequest, err)213 }214 respondError(rw, http.StatusInternalServerError, err)215 return216 }217 protectedData, err := o.ProtectService.Get(r.Context(), t.DID)218 if err != nil {219 respondError(rw, http.StatusInternalServerError, err)220 return221 }222 if _, err = o.checkPolicy(r.Context(), protectedData.PolicyID, policy.Handler); err != nil {223 respondError(rw, err.(*policyError).status, err) //nolint:errorlint,forcetypeassert224 return225 }226 respond(rw, http.StatusOK, &TicketStatusResponse{Status: t.Status.String()})227}228// collectHandler swagger:route POST /v1/release/{ticket_id}/collect gatekeeper collectReq229//230// Generates extract query for the ticket that has completed authorization process.231//232// Authorization: HTTP Signatures (headers="(request-target) date")233//234// Responses:235// 200: collectResp236// default: errorResp237func (o *Operation) collectHandler(rw http.ResponseWriter, r *http.Request) {238 ticketID := strings.ToLower(mux.Vars(r)[ticketIDVarName])239 t, err := o.ReleaseService.Get(r.Context(), ticketID)240 if err != nil {241 respondError(rw, http.StatusBadRequest, err)242 return243 }244 protectedData, err := o.ProtectService.Get(r.Context(), t.DID)245 if err != nil {246 respondError(rw, http.StatusInternalServerError, err)247 return248 }249 if t.Status != ticket.ReadyToCollect {250 respondError(rw, http.StatusUnauthorized, errors.New("not authorized to access ticket"))251 return252 }253 subDID, err := o.checkPolicy(r.Context(), protectedData.PolicyID, policy.Handler)...

Full Screen

Full Screen

service_test.go

Source:service_test.go Github

copy

Full Screen

...51 require.NoError(t, err)52 require.NotNil(t, svc)53 })54}55func TestService_Release(t *testing.T) {56 t.Run("Fail to store ticket", func(t *testing.T) {57 store := storage.NewMockStoreProvider()58 store.Store.ErrPut = errors.New("put error")59 svc, err := release.NewService(&release.Config{60 StoreProvider: store,61 })62 require.NoError(t, err)63 ticket, err := svc.Release(context.Background(), testDID)64 require.EqualError(t, err, "store ticket: put error")65 require.Nil(t, ticket)66 })67 t.Run("Success", func(t *testing.T) {68 svc, err := release.NewService(&release.Config{69 StoreProvider: storage.NewMockStoreProvider(),70 })71 require.NoError(t, err)72 ticket, err := svc.Release(context.Background(), testDID)73 require.NoError(t, err)74 require.NotNil(t, ticket)75 })76}77func TestService_Get(t *testing.T) {78 t.Run("Fail to get ticket", func(t *testing.T) {79 store := storage.NewMockStoreProvider()80 store.Store.ErrGet = errors.New("get error")81 svc, err := release.NewService(&release.Config{82 StoreProvider: store,83 })84 require.NoError(t, err)85 ticket, err := svc.Get(context.Background(), testTicketID)86 require.EqualError(t, err, "get ticket: get error")...

Full Screen

Full Screen

Release

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 wg.Add(1)4 go func() {5 defer wg.Done()6 fmt.Println("Hello")7 }()8 wg.Wait()9}

Full Screen

Full Screen

Release

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 wg.Add(1)4 go func() {5 defer wg.Done()6 for i := 0; i < 3; i++ {7 protect.Do(func() {8 fmt.Println("Hello World")9 })10 fmt.Println("Hello")11 }12 }()13 wg.Wait()14}

Full Screen

Full Screen

Release

Using AI Code Generation

copy

Full Screen

1func main() {2 p := protect.New(1234)3 defer p.Release()4}5func main() {6 p := protect.New(1234)7 defer p.Release()8}

Full Screen

Full Screen

Release

Using AI Code Generation

copy

Full Screen

1import (2type protect struct {3}4func (p *protect) Release() {5 p.Unlock()6}7func main() {8 p.Lock()9 var x interface{} = &p10 release, ok := x.(interface{ Release() })11 if !ok {12 fmt.Println("not ok")13 }14 release.Release()15}16The type assertion x.(T) is equivalent to a type switch with a single case T. The type switch is more general, and it works in this case:17import (18type protect struct {19}20func (p *protect) Release() {21 p.Unlock()22}23func main() {24 p.Lock()25 var x interface{} = &p26 switch release := x.(type) {27 case interface{ Release() }:28 release.Release()29 fmt.Println("not ok")30 }31}

Full Screen

Full Screen

Release

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p := protect.NewProtect()4 p.Release()5 fmt.Println("Hello World")6}7import (8type Protect struct {9}10func NewProtect() *Protect {11 return &Protect{}12}13func (p *Protect) Release() {14 fmt.Println("Release")15}16import (17func TestRelease(t *testing.T) {18 p := NewProtect()19 p.Release()20}

Full Screen

Full Screen

Release

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 runtime.GOMAXPROCS(1)4 protect.Set(1)5 fmt.Println(protect)6 defer protect.Release()7 fmt.Println(protect)8}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selenoid automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful