Skip to content

Commit

Permalink
fix: Add coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Aaron <[email protected]>
  • Loading branch information
andaaron committed Mar 15, 2024
1 parent 9853a53 commit 1491bd4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/server/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ func readLDAPCredentials(ldapConfigPath string) (config.LDAPCredentials, error)
var ldapCredentials config.LDAPCredentials

metaData := &mapstructure.Metadata{}
if err := viperInstance.UnmarshalExact(&ldapCredentials, metadataConfig(metaData)); err != nil {
if err := viperInstance.Unmarshal(&ldapCredentials, metadataConfig(metaData)); err != nil {
log.Error().Err(err).Msg("failed to unmarshal ldap credentials config")

Check warning on line 867 in pkg/cli/server/root.go

View check run for this annotation

Codecov / codecov/patch

pkg/cli/server/root.go#L867

Added line #L867 was not covered by tests

return config.LDAPCredentials{}, errors.Join(zerr.ErrBadConfig, err)

Check warning on line 869 in pkg/cli/server/root.go

View check run for this annotation

Codecov / codecov/patch

pkg/cli/server/root.go#L869

Added line #L869 was not covered by tests
Expand Down
54 changes: 45 additions & 9 deletions pkg/cli/server/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ storage:
content := []byte(`{"distSpecVersion":"1.1.0","storage":{"rootDirectory":"/tmp/zot"},
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
"auth":{"openid":{"providers":{"oidc":{"issuer":"http://127.0.0.1:5556/dex",
"clientid":"client_id","scopes":["openid"]}}}}},
"clientid":"client_id","scopes":["openid"]}}}}},
"log":{"level":"debug"}}`)
_, err = tmpfile.Write(content)
So(err, ShouldBeNil)
Expand Down Expand Up @@ -1247,8 +1247,8 @@ storage:
defer os.Remove(tmpCredsFile.Name())

content := []byte(`{
"bindDN":"cn=ldap-searcher,ou=Users,dc=example,dc=org",
"bindPassword":"ldap-searcher-password"
"bindDN":"cn=ldap-searcher,ou=Users,dc=example,dc=org",
"bindPassword":"ldap-searcher-password"
}`)

_, err = tmpCredsFile.Write(content)
Expand Down Expand Up @@ -1286,7 +1286,7 @@ storage:

// `bindDN` key is missing
content := []byte(`{
"bindPassword":"ldap-searcher-password"
"bindPassword":"ldap-searcher-password"
}`)

_, err = tmpCredsFile.Write(content)
Expand Down Expand Up @@ -1323,10 +1323,9 @@ storage:
So(err, ShouldBeNil)
defer os.Remove(tmpCredsFile.Name())

// `bindDN` key is missing
content := []byte(`{
"bindDN":"cn=ldap-searcher,ou=Users,dc=example,dc=org",
"bindPassword":"ldap-searcher-password",
"bindDN":"cn=ldap-searcher,ou=Users,dc=example,dc=org",
"bindPassword":"ldap-searcher-password",
"extraKey": "extraValue"
}`)

Expand Down Expand Up @@ -1355,7 +1354,7 @@ storage:
So(err.Error(), ShouldContainSubstring, "invalid server config")
})

Convey("Test verify bad ldap config: no keys set", t, func(c C) {
Convey("Test verify bad ldap config: empty credentials file", t, func(c C) {
tmpFile, err := os.CreateTemp("", "zot-test*.json")
So(err, ShouldBeNil)
defer os.Remove(tmpFile.Name())
Expand Down Expand Up @@ -1391,6 +1390,43 @@ storage:
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "invalid server config")
})

Convey("Test verify bad ldap config: no keys set in credentials file", t, func(c C) {
tmpFile, err := os.CreateTemp("", "zot-test*.json")
So(err, ShouldBeNil)
defer os.Remove(tmpFile.Name())

tmpCredsFile, err := os.CreateTemp("", "zot-cred*.json")
So(err, ShouldBeNil)
defer os.Remove(tmpCredsFile.Name())

// empty json
content := []byte(`{}`)

_, err = tmpCredsFile.Write(content)
So(err, ShouldBeNil)
err = tmpCredsFile.Close()
So(err, ShouldBeNil)

content = []byte(fmt.Sprintf(`{ "distSpecVersion": "1.1.0-dev",
"storage": { "rootDirectory": "/tmp/zot" }, "http": { "address": "127.0.0.1", "port": "8080",
"auth": { "ldap": { "credentialsFile": "%v", "address": "ldap.example.org", "port": 389,
"startTLS": false, "baseDN": "ou=Users,dc=example,dc=org",
"userAttribute": "uid", "userGroupAttribute": "memberOf", "skipVerify": true, "subtreeSearch": true },
"failDelay": 5 } }, "log": { "level": "debug" } }`,
tmpCredsFile.Name()),
)

_, err = tmpFile.Write(content)
So(err, ShouldBeNil)
err = tmpFile.Close()
So(err, ShouldBeNil)

os.Args = []string{"cli_test", "verify", tmpFile.Name()}
err = cli.NewServerRootCmd().Execute()
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "invalid server config")
})
}

func TestApiKeyConfig(t *testing.T) {
Expand All @@ -1403,7 +1439,7 @@ func TestApiKeyConfig(t *testing.T) {
content := []byte(`{"distSpecVersion":"1.1.0","storage":{"rootDirectory":"/tmp/zot"},
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
"auth":{"openid":{"providers":{"oidc":{"issuer":"http://127.0.0.1:5556/dex",
"clientid":"client_id","scopes":["openid"]}}}}},
"clientid":"client_id","scopes":["openid"]}}}}},
"log":{"level":"debug"}}`)

err = os.WriteFile(tmpfile.Name(), content, 0o0600)
Expand Down

0 comments on commit 1491bd4

Please sign in to comment.