github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/model/bitwarden/cipher_test.go (about)

     1  package bitwarden
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/cozy/cozy-stack/model/instance"
     9  	"github.com/cozy/cozy-stack/model/instance/lifecycle"
    10  	"github.com/cozy/cozy-stack/model/stack"
    11  	"github.com/cozy/cozy-stack/pkg/config/config"
    12  	"github.com/cozy/cozy-stack/pkg/consts"
    13  	"github.com/cozy/cozy-stack/pkg/couchdb"
    14  	"github.com/cozy/cozy-stack/pkg/metadata"
    15  	"github.com/cozy/cozy-stack/tests/testutils"
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  func TestCipher(t *testing.T) {
    21  	if testing.Short() {
    22  		t.Skip("an instance is required for this test: test skipped due to the use of --short flag")
    23  	}
    24  
    25  	config.UseTestFile(t)
    26  	testutils.NeedCouchdb(t)
    27  
    28  	_, _, err := stack.Start()
    29  	require.NoError(t, err, "Error while starting the job system")
    30  
    31  	t.Run("DeleteUnrecoverableCiphers", func(t *testing.T) {
    32  		domain := "cozy.example.net"
    33  		err := lifecycle.Destroy(domain)
    34  		if !errors.Is(err, instance.ErrNotFound) {
    35  			assert.NoError(t, err)
    36  		}
    37  		inst, err := lifecycle.Create(&lifecycle.Options{
    38  			Domain:     domain,
    39  			Passphrase: "cozy",
    40  			PublicName: "Pierre",
    41  		})
    42  		assert.NoError(t, err)
    43  		defer func() {
    44  			_ = lifecycle.Destroy(inst.Domain)
    45  		}()
    46  
    47  		for i := 0; i < 5; i++ {
    48  			md := metadata.New()
    49  			md.DocTypeVersion = DocTypeVersion
    50  			cipher := &Cipher{
    51  				Type:           SecureNoteType,
    52  				SharedWithCozy: i%2 == 0,
    53  				Favorite:       i%3 == 0,
    54  				Name:           fmt.Sprintf("2.%d|%d|%d", i, i, i),
    55  				Metadata:       md,
    56  			}
    57  			assert.NoError(t, couchdb.CreateDoc(inst, cipher))
    58  		}
    59  
    60  		assert.NoError(t, DeleteUnrecoverableCiphers(inst))
    61  		var ciphers []*Cipher
    62  		err = couchdb.GetAllDocs(inst, consts.BitwardenCiphers, nil, &ciphers)
    63  		assert.NoError(t, err)
    64  		assert.Len(t, ciphers, 3)
    65  		for _, c := range ciphers {
    66  			assert.True(t, c.SharedWithCozy)
    67  		}
    68  	})
    69  }