2017-07-26 18:03:43 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-02-27 19:28:31 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2017-07-26 18:03:43 +00:00
|
|
|
)
|
|
|
|
|
2017-08-03 22:39:31 +00:00
|
|
|
func TestStore_RegularTokens(t *testing.T) {
|
2017-07-26 18:03:43 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-08-03 22:39:31 +00:00
|
|
|
type tokens struct {
|
2019-02-27 19:28:31 +00:00
|
|
|
userSource TokenSource
|
|
|
|
user string
|
|
|
|
agent string
|
|
|
|
agentSource TokenSource
|
|
|
|
master string
|
|
|
|
masterSource TokenSource
|
|
|
|
repl string
|
|
|
|
replSource TokenSource
|
2017-08-03 22:39:31 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 18:03:43 +00:00
|
|
|
tests := []struct {
|
2017-08-03 22:39:31 +00:00
|
|
|
name string
|
2019-02-27 19:28:31 +00:00
|
|
|
set tokens
|
|
|
|
raw tokens
|
|
|
|
effective tokens
|
2017-07-26 18:03:43 +00:00
|
|
|
}{
|
2017-08-03 22:39:31 +00:00
|
|
|
{
|
2019-02-27 19:28:31 +00:00
|
|
|
name: "set user - config",
|
|
|
|
set: tokens{user: "U", userSource: TokenSourceConfig},
|
|
|
|
raw: tokens{user: "U", userSource: TokenSourceConfig},
|
|
|
|
effective: tokens{user: "U", agent: "U"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "set user - api",
|
|
|
|
set: tokens{user: "U", userSource: TokenSourceAPI},
|
|
|
|
raw: tokens{user: "U", userSource: TokenSourceAPI},
|
|
|
|
effective: tokens{user: "U", agent: "U"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "set agent - config",
|
|
|
|
set: tokens{agent: "A", agentSource: TokenSourceConfig},
|
|
|
|
raw: tokens{agent: "A", agentSource: TokenSourceConfig},
|
|
|
|
effective: tokens{agent: "A"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "set agent - api",
|
|
|
|
set: tokens{agent: "A", agentSource: TokenSourceAPI},
|
|
|
|
raw: tokens{agent: "A", agentSource: TokenSourceAPI},
|
|
|
|
effective: tokens{agent: "A"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "set user and agent",
|
|
|
|
set: tokens{agent: "A", user: "U"},
|
|
|
|
raw: tokens{agent: "A", user: "U"},
|
|
|
|
effective: tokens{agent: "A", user: "U"},
|
2017-08-03 22:39:31 +00:00
|
|
|
},
|
|
|
|
{
|
2019-02-27 19:28:31 +00:00
|
|
|
name: "set repl - config",
|
|
|
|
set: tokens{repl: "R", replSource: TokenSourceConfig},
|
|
|
|
raw: tokens{repl: "R", replSource: TokenSourceConfig},
|
|
|
|
effective: tokens{repl: "R"},
|
2017-08-03 22:39:31 +00:00
|
|
|
},
|
|
|
|
{
|
2019-02-27 19:28:31 +00:00
|
|
|
name: "set repl - api",
|
|
|
|
set: tokens{repl: "R", replSource: TokenSourceAPI},
|
|
|
|
raw: tokens{repl: "R", replSource: TokenSourceAPI},
|
|
|
|
effective: tokens{repl: "R"},
|
2017-08-03 22:39:31 +00:00
|
|
|
},
|
|
|
|
{
|
2019-02-27 19:28:31 +00:00
|
|
|
name: "set master - config",
|
|
|
|
set: tokens{master: "M", masterSource: TokenSourceConfig},
|
|
|
|
raw: tokens{master: "M", masterSource: TokenSourceConfig},
|
|
|
|
effective: tokens{master: "M"},
|
2017-08-03 22:39:31 +00:00
|
|
|
},
|
|
|
|
{
|
2019-02-27 19:28:31 +00:00
|
|
|
name: "set master - api",
|
|
|
|
set: tokens{master: "M", masterSource: TokenSourceAPI},
|
|
|
|
raw: tokens{master: "M", masterSource: TokenSourceAPI},
|
|
|
|
effective: tokens{master: "M"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "set all",
|
|
|
|
set: tokens{user: "U", agent: "A", repl: "R", master: "M"},
|
|
|
|
raw: tokens{user: "U", agent: "A", repl: "R", master: "M"},
|
|
|
|
effective: tokens{user: "U", agent: "A", repl: "R", master: "M"},
|
2017-08-03 22:39:31 +00:00
|
|
|
},
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2017-08-03 22:39:31 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-02-27 19:28:31 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-08-03 22:39:31 +00:00
|
|
|
s := new(Store)
|
2019-10-04 18:37:34 +00:00
|
|
|
require.True(t, s.UpdateUserToken(tt.set.user, tt.set.userSource))
|
|
|
|
require.True(t, s.UpdateAgentToken(tt.set.agent, tt.set.agentSource))
|
|
|
|
require.True(t, s.UpdateReplicationToken(tt.set.repl, tt.set.replSource))
|
|
|
|
require.True(t, s.UpdateAgentMasterToken(tt.set.master, tt.set.masterSource))
|
|
|
|
|
|
|
|
// If they don't change then they return false.
|
|
|
|
require.False(t, s.UpdateUserToken(tt.set.user, tt.set.userSource))
|
|
|
|
require.False(t, s.UpdateAgentToken(tt.set.agent, tt.set.agentSource))
|
|
|
|
require.False(t, s.UpdateReplicationToken(tt.set.repl, tt.set.replSource))
|
|
|
|
require.False(t, s.UpdateAgentMasterToken(tt.set.master, tt.set.masterSource))
|
2019-02-27 19:28:31 +00:00
|
|
|
|
|
|
|
require.Equal(t, tt.effective.user, s.UserToken())
|
|
|
|
require.Equal(t, tt.effective.agent, s.AgentToken())
|
|
|
|
require.Equal(t, tt.effective.master, s.AgentMasterToken())
|
|
|
|
require.Equal(t, tt.effective.repl, s.ReplicationToken())
|
|
|
|
|
|
|
|
tok, src := s.UserTokenAndSource()
|
|
|
|
require.Equal(t, tt.raw.user, tok)
|
|
|
|
require.Equal(t, tt.raw.userSource, src)
|
|
|
|
|
|
|
|
tok, src = s.AgentTokenAndSource()
|
|
|
|
require.Equal(t, tt.raw.agent, tok)
|
|
|
|
require.Equal(t, tt.raw.agentSource, src)
|
|
|
|
|
|
|
|
tok, src = s.AgentMasterTokenAndSource()
|
|
|
|
require.Equal(t, tt.raw.master, tok)
|
|
|
|
require.Equal(t, tt.raw.masterSource, src)
|
|
|
|
|
|
|
|
tok, src = s.ReplicationTokenAndSource()
|
|
|
|
require.Equal(t, tt.raw.repl, tok)
|
|
|
|
require.Equal(t, tt.raw.replSource, src)
|
2017-08-03 22:39:31 +00:00
|
|
|
})
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_AgentMasterToken(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-08-03 22:39:31 +00:00
|
|
|
s := new(Store)
|
2017-07-26 18:03:43 +00:00
|
|
|
|
|
|
|
verify := func(want bool, toks ...string) {
|
|
|
|
for _, tok := range toks {
|
2019-02-27 19:28:31 +00:00
|
|
|
require.Equal(t, want, s.IsAgentMasterToken(tok))
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
verify(false, "", "nope")
|
|
|
|
|
2019-02-27 19:28:31 +00:00
|
|
|
s.UpdateAgentMasterToken("master", TokenSourceConfig)
|
2017-07-26 18:03:43 +00:00
|
|
|
verify(true, "master")
|
|
|
|
verify(false, "", "nope")
|
|
|
|
|
2019-02-27 19:28:31 +00:00
|
|
|
s.UpdateAgentMasterToken("another", TokenSourceConfig)
|
2017-07-26 18:03:43 +00:00
|
|
|
verify(true, "another")
|
|
|
|
verify(false, "", "nope", "master")
|
|
|
|
|
2019-02-27 19:28:31 +00:00
|
|
|
s.UpdateAgentMasterToken("", TokenSourceConfig)
|
2017-07-26 18:03:43 +00:00
|
|
|
verify(false, "", "nope", "master", "another")
|
|
|
|
}
|