github.com/bndr/gojenkins@v1.1.0/credentials_test.go (about)

     1  package gojenkins
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  var (
    12  	cm         CredentialsManager
    13  	domain     = "_"
    14  	dockerID   = "dockerIDCred"
    15  	sshID      = "sshIdCred"
    16  	usernameID = "usernameIDcred"
    17  	scope      = "GLOBAL"
    18  )
    19  
    20  func TestCreateUsernameCredentials(t *testing.T) {
    21  
    22  	cred := UsernameCredentials{
    23  		ID:       usernameID,
    24  		Scope:    scope,
    25  		Username: "usernameTest",
    26  		Password: "pass",
    27  	}
    28  
    29  	ctx := context.Background()
    30  	err := cm.Add(ctx, domain, cred)
    31  	assert.Nil(t, err, "Could not create credential")
    32  
    33  	getCred := UsernameCredentials{}
    34  	err = cm.GetSingle(ctx, domain, cred.ID, &getCred)
    35  	assert.Nil(t, err, "Could not get credential")
    36  
    37  	assert.Equal(t, cred.Scope, getCred.Scope, "Scope is not equal")
    38  	assert.Equal(t, cred.ID, cred.ID, "ID is not equal")
    39  	assert.Equal(t, cred.Username, cred.Username, "Username is not equal")
    40  }
    41  
    42  func TestCreateDockerCredentials(t *testing.T) {
    43  
    44  	cred := DockerServerCredentials{
    45  		Scope:             scope,
    46  		ID:                dockerID,
    47  		Username:          "docker-name",
    48  		ClientCertificate: "some secret value",
    49  		ClientKey:         "client key",
    50  	}
    51  
    52  	ctx := context.Background()
    53  	err := cm.Add(ctx, domain, cred)
    54  	assert.Nil(t, err, "Could not create credential")
    55  
    56  	getCred := DockerServerCredentials{}
    57  	err = cm.GetSingle(ctx, domain, cred.ID, &getCred)
    58  	assert.Nil(t, err, "Could not get credential")
    59  
    60  	assert.Equal(t, cred.Scope, getCred.Scope, "Scope is not equal")
    61  	assert.Equal(t, cred.ID, cred.ID, "ID is not equal")
    62  	assert.Equal(t, cred.Username, cred.Username, "Username is not equal")
    63  	assert.Equal(t, cred.ClientCertificate, cred.ClientCertificate, "ClientCertificate is not equal")
    64  	assert.Equal(t, cred.ClientKey, cred.ClientKey, "Username is not equal")
    65  
    66  }
    67  
    68  func TestCreateSSHCredentialsFullFlow(t *testing.T) {
    69  	sshCred := SSHCredentials{
    70  		Scope:      scope,
    71  		ID:         sshID,
    72  		Username:   "RANDONMANE",
    73  		Passphrase: "password",
    74  		PrivateKeySource: &PrivateKeyFile{
    75  			Value: "testValueofkey",
    76  			Class: KeySourceOnMasterType,
    77  		},
    78  	}
    79  
    80  	ctx := context.Background()
    81  	err := cm.Add(ctx, domain, sshCred)
    82  	assert.Nil(t, err, "Could not create credential")
    83  
    84  	sshCred.Username = "new_username"
    85  	err = cm.Update(ctx, domain, sshCred.ID, sshCred)
    86  	assert.Nil(t, err, "Could not update credential")
    87  
    88  	getSSH := SSHCredentials{}
    89  	err = cm.GetSingle(ctx, domain, sshCred.ID, &getSSH)
    90  	assert.Nil(t, err, "Could not get ssh credential")
    91  
    92  	assert.Equal(t, sshCred.Scope, getSSH.Scope, "Scope is not equal")
    93  	assert.Equal(t, sshCred.ID, getSSH.ID, "ID is not equal")
    94  	assert.Equal(t, sshCred.Username, getSSH.Username, "Username is not equal")
    95  	assert.Equal(t, sshCred.Scope, getSSH.Scope, "Scope is not equal")
    96  
    97  	err = cm.Delete(ctx, domain, getSSH.ID)
    98  	assert.Nil(t, err, "Could not delete credentials")
    99  
   100  }
   101  
   102  func TestMain(m *testing.M) {
   103  	//setup
   104  	ctx := context.Background()
   105  	jenkins := CreateJenkins(nil, "http://localhost:8080", "admin", "admin")
   106  	jenkins.Init(ctx)
   107  
   108  	cm = CredentialsManager{J: jenkins}
   109  
   110  	//execute tests
   111  	os.Exit(m.Run())
   112  }