Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Add unit tests to RFP-related functions #1203

Merged
merged 9 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion politeiad/cache/testcache/decred.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (c *testcache) findLinkedFrom(token string) ([]string, error) {
// provided token.
for _, allVersions := range c.records {
// Get the latest version of the proposal
r := allVersions[string(len(allVersions))]
r := allVersions[strconv.Itoa(len(allVersions))]

// Extract LinkTo from the ProposalMetadata file
for _, f := range r.Files {
Expand Down
73 changes: 73 additions & 0 deletions politeiad/testpoliteiad/decred.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,84 @@ func (p *TestPoliteiad) startVote(payload string) (string, error) {
return string(svrb), nil
}

func (p *TestPoliteiad) startVoteRunoff(payload string) (string, error) {
svr, err := decred.DecodeStartVoteRunoff([]byte(payload))
if err != nil {
return "", err
}

p.Lock()
defer p.Unlock()

// Store authorize votes
avReply := make(map[string]decred.AuthorizeVoteReply)
for _, av := range svr.AuthorizeVotes {
r, err := p.record(av.Token)
if err != nil {
return "", err
}
// Fill client data
s := p.identity.SignMessage([]byte(av.Signature))
av.Version = decred.VersionAuthorizeVote
av.Receipt = hex.EncodeToString(s[:])
av.Timestamp = time.Now().Unix()
av.Version = decred.VersionAuthorizeVote

// Store
_, ok := p.authorizeVotes[av.Token]
if !ok {
p.authorizeVotes[av.Token] = make(map[string]decred.AuthorizeVote)
}
p.authorizeVotes[av.Token][r.Version] = av

// Prepare response
avr := decred.AuthorizeVoteReply{
Action: av.Action,
RecordVersion: r.Version,
Receipt: av.Receipt,
Timestamp: av.Timestamp,
}
avReply[av.Token] = avr
}

// Store start votes
svReply := decred.StartVoteReply{}
for _, sv := range svr.StartVotes {
sv.Version = decred.VersionStartVote
p.startVotes[sv.Vote.Token] = sv
// Prepare response
endHeight := bestBlock + sv.Vote.Duration
svReply.Version = decred.VersionStartVoteReply
svReply.StartBlockHeight = strconv.FormatUint(uint64(bestBlock), 10)
svReply.EndHeight = strconv.FormatUint(uint64(endHeight), 10)
svReply.EligibleTickets = []string{}
}

// Store start vote runoff
p.startVotesRunoff[svr.Token] = *svr

response := decred.StartVoteRunoffReply{
AuthorizeVoteReplies: avReply,
StartVoteReply: svReply,
}

p.startVotesRunoffReplies[svr.Token] = response

svrReply, err := decred.EncodeStartVoteRunoffReply(response)
if err != nil {
return "", err
}

return string(svrReply), nil
}

// decredExec executes the passed in plugin command.
func (p *TestPoliteiad) decredExec(pc v1.PluginCommand) (string, error) {
switch pc.Command {
case decred.CmdStartVote:
return p.startVote(pc.Payload)
case decred.CmdStartVoteRunoff:
return p.startVoteRunoff(pc.Payload)
case decred.CmdAuthorizeVote:
return p.authorizeVote(pc.Payload)
case decred.CmdBestBlock:
Expand Down
33 changes: 20 additions & 13 deletions politeiad/testpoliteiad/testpoliteiad.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,20 @@ type TestPoliteiad struct {

URL string // Base url of form http://ipaddr:port
PublicIdentity *identity.PublicIdentity
FullIdentity *identity.FullIdentity

identity *identity.FullIdentity
server *httptest.Server
cache cache.Cache
records map[string]map[string]v1.Record // [token][version]Record

// Decred plugin
authorizeVotes map[string]map[string]decred.AuthorizeVote // [token][version]AuthorizeVote
startVotes map[string]decred.StartVoteV2 // [token]StartVote
startVoteReplies map[string]decred.StartVoteReply // [token]StartVoteReply
authorizeVotes map[string]map[string]decred.AuthorizeVote // [token][version]AuthorizeVote
startVotes map[string]decred.StartVoteV2 // [token]StartVote
startVoteReplies map[string]decred.StartVoteReply // [token]StartVoteReply
startVotesRunoff map[string]decred.StartVoteRunoff // [token]StartVoteRunoff
startVotesRunoffReplies map[string]decred.StartVoteRunoffReply // [token]StartVoteRunoffReply

}

func respondWithUserError(w http.ResponseWriter,
Expand Down Expand Up @@ -173,17 +177,17 @@ func (p *TestPoliteiad) handleNewRecord(w http.ResponseWriter, r *http.Request)
return
}

merkle, err := merkleRoot(t.Files)
mr, err := merkleRoot(t.Files)
if err != nil {
util.RespondWithJSON(w, http.StatusInternalServerError, err)
return
}

token := hex.EncodeToString(tokenb)
sig := p.identity.SignMessage([]byte(merkle + token))
sig := p.identity.SignMessage([]byte(mr + token))
resp := p.identity.SignMessage(challenge)
cr := v1.CensorshipRecord{
Merkle: merkle,
Merkle: mr,
Token: token,
Signature: hex.EncodeToString(sig[:]),
}
Expand Down Expand Up @@ -481,13 +485,16 @@ func New(t *testing.T, c cache.Cache) *TestPoliteiad {

// Init context
p := TestPoliteiad{
PublicIdentity: &id.Public,
identity: id,
cache: c,
records: make(map[string]map[string]v1.Record),
authorizeVotes: make(map[string]map[string]decred.AuthorizeVote),
startVotes: make(map[string]decred.StartVoteV2),
startVoteReplies: make(map[string]decred.StartVoteReply),
FullIdentity: id,
PublicIdentity: &id.Public,
identity: id,
cache: c,
records: make(map[string]map[string]v1.Record),
authorizeVotes: make(map[string]map[string]decred.AuthorizeVote),
startVotes: make(map[string]decred.StartVoteV2),
startVoteReplies: make(map[string]decred.StartVoteReply),
startVotesRunoff: make(map[string]decred.StartVoteRunoff),
startVotesRunoffReplies: make(map[string]decred.StartVoteRunoffReply),
}

// Setup routes
Expand Down
6 changes: 1 addition & 5 deletions politeiawww/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,7 @@ func (e *EventManager) _register(eventType EventT, listenerToAdd chan interface{
e.Listeners = make(map[EventT][]chan interface{})
}

if _, ok := e.Listeners[eventType]; ok {
e.Listeners[eventType] = append(e.Listeners[eventType], listenerToAdd)
} else {
e.Listeners[eventType] = []chan interface{}{listenerToAdd}
}
e.Listeners[eventType] = append(e.Listeners[eventType], listenerToAdd)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gofmt linter was requesting this code change: https://staticcheck.io/docs/checks#S1036

}

// _unregister removes the given listener channel for the given event type.
Expand Down
7 changes: 3 additions & 4 deletions politeiawww/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -2448,7 +2448,7 @@ func validateAuthorizeVote(av www.AuthorizeVote, u user.User, pr www.ProposalRec
return nil
}

// validateAuthorizeVoteRunoff validates the authorize vote for a proposal that
// validateAuthorizeVoteStandard validates the authorize vote for a proposal that
// is participating in a standard vote. A UserError is returned if any of the
// validation fails.
func validateAuthorizeVoteStandard(av www.AuthorizeVote, u user.User, pr www.ProposalRecord, vs www.VoteSummary) error {
Expand Down Expand Up @@ -2738,7 +2738,6 @@ func validateStartVoteStandard(sv www2.StartVote, u user.User, pr www.ProposalRe
}

// The remaining validation is specific to a VoteTypeStandard.

switch {
case sv.Vote.Type != www2.VoteTypeStandard:
// Not a standard vote
Expand Down Expand Up @@ -2832,7 +2831,7 @@ func validateStartVoteRunoff(sv www2.StartVote, u user.User, pr www.ProposalReco

case !isRFPSubmission(pr):
// The proposal is not an RFP submission
e := fmt.Sprintf("%v in not an rfp submission", token)
e := fmt.Sprintf("%v is not an rfp submission", token)
return www.UserError{
ErrorCode: www.ErrorStatusWrongProposalType,
ErrorContext: []string{e},
Expand Down Expand Up @@ -3027,7 +3026,7 @@ func (p *politeiawww) processStartVoteRunoffV2(sv www2.StartVoteRunoff, u *user.
}
}
if len(auths) == 0 {
e := fmt.Sprintf("start votes and authorize votes cannot be empty")
e := "start votes and authorize votes cannot be empty"
return nil, www.UserError{
ErrorCode: www.ErrorStatusInvalidRunoffVote,
ErrorContext: []string{e},
Expand Down
Loading