Refactor public API tests for messages

- move tests for public API to messages_test package
This commit is contained in:
Jan Dittberner 2022-12-04 19:21:45 +01:00
parent 51ca9cc69d
commit a0b6fdce98
2 changed files with 98 additions and 69 deletions

View file

@ -0,0 +1,98 @@
/*
Copyright 2022 CAcert Inc.
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package messages_test
import (
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"git.cacert.org/cacert-gosigner/pkg/messages"
)
func TestParseUsage(t *testing.T) {
okValues := []string{
"ocsp", "client", "code", "person", "server", "server_client",
"org_client", "org_code", "org_email", "org_person", "org_server", "org_server_client",
}
for _, v := range okValues {
t.Run(v, func(t *testing.T) {
u, err := messages.ParseUsage(v)
assert.NoError(t, err)
assert.Greater(t, u, messages.ProfileUsage(0))
})
}
t.Run("invalid", func(t *testing.T) {
u, err := messages.ParseUsage("foo")
assert.Error(t, err)
assert.Equal(t, u, messages.UsageInvalid)
})
}
func TestBuildCommandAnnounce(t *testing.T) {
commands := []messages.CommandCode{
messages.CmdUndef,
messages.CmdHealth,
messages.CmdFetchCRL,
}
for _, c := range commands {
t.Run(c.String(), func(t *testing.T) {
announce := messages.BuildCommandAnnounce(c)
require.NotNil(t, announce)
assert.Equal(t, c, announce.Code)
assert.NotEmpty(t, announce.ID)
assert.NotEmpty(t, announce.Created)
assert.True(t, announce.Created.Before(time.Now().UTC()))
})
}
}
func TestBuildResponseAnnounce(t *testing.T) {
responses := []messages.ResponseCode{
messages.RespError,
messages.RespUndef,
messages.RespHealth,
messages.RespFetchCRL,
}
for _, r := range responses {
commandID := uuid.NewString()
t.Run(r.String(), func(t *testing.T) {
announce := messages.BuildResponseAnnounce(r, commandID)
assert.NotNil(t, announce)
assert.Equal(t, r, announce.Code)
assert.Equal(t, commandID, announce.ID)
assert.NotEmpty(t, announce.ID)
assert.NotEmpty(t, announce.Created)
assert.True(t, announce.Created.Before(time.Now().UTC()))
})
}
}

View file

@ -92,27 +92,6 @@ func TestResponseCode_String(t *testing.T) {
})
}
func TestBuildCommandAnnounce(t *testing.T) {
commands := []CommandCode{
CmdUndef,
CmdHealth,
CmdFetchCRL,
}
for _, c := range commands {
t.Run(c.String(), func(t *testing.T) {
announce := BuildCommandAnnounce(c)
require.NotNil(t, announce)
assert.Equal(t, c, announce.Code)
assert.NotEmpty(t, announce.ID)
assert.NotEmpty(t, announce.Created)
assert.True(t, announce.Created.Before(time.Now().UTC()))
})
}
}
func TestCommandAnnounce_String(t *testing.T) {
announce := BuildCommandAnnounce(CmdUndef)
@ -126,31 +105,6 @@ func TestCommandAnnounce_String(t *testing.T) {
assert.Contains(t, str, "created")
}
func TestBuildResponseAnnounce(t *testing.T) {
responses := []ResponseCode{
RespError,
RespUndef,
RespHealth,
RespFetchCRL,
}
for _, r := range responses {
commandID := uuid.NewString()
t.Run(r.String(), func(t *testing.T) {
announce := BuildResponseAnnounce(r, commandID)
assert.NotNil(t, announce)
assert.Equal(t, r, announce.Code)
assert.Equal(t, commandID, announce.ID)
assert.NotEmpty(t, announce.ID)
assert.NotEmpty(t, announce.Created)
assert.True(t, announce.Created.Before(time.Now().UTC()))
})
}
}
func TestResponseAnnounce_String(t *testing.T) {
announce := BuildResponseAnnounce(RespUndef, uuid.NewString())
@ -276,29 +230,6 @@ func TestProfileUsage_Description(t *testing.T) {
})
}
func TestParseUsage(t *testing.T) {
okValues := []string{
"ocsp", "client", "code", "person", "server", "server_client",
"org_client", "org_code", "org_email", "org_person", "org_server", "org_server_client",
}
for _, v := range okValues {
t.Run(v, func(t *testing.T) {
u, err := ParseUsage(v)
assert.NoError(t, err)
assert.Greater(t, u, ProfileUsage(0))
})
}
t.Run("invalid", func(t *testing.T) {
u, err := ParseUsage("foo")
assert.Error(t, err)
assert.Equal(t, u, UsageInvalid)
})
}
func TestCAProfile_String(t *testing.T) {
profiles := []CAProfile{
{Name: "test-client", UseFor: UsageClient},