Skip to content

Commit

Permalink
politeiawww: Add unit tests to RFP functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
thi4go committed Jun 30, 2020
1 parent 8aaf8f5 commit 6ee629d
Show file tree
Hide file tree
Showing 7 changed files with 3,237 additions and 1,115 deletions.
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)
}

// _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 @@ -2442,7 +2442,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 @@ -2732,7 +2732,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 @@ -2826,7 +2825,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 @@ -3021,7 +3020,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

0 comments on commit 6ee629d

Please sign in to comment.