Skip to content

Commit

Permalink
Vote start requirements (#113)
Browse files Browse the repository at this point in the history
* Change /create to /start
* Remove "started" check
  • Loading branch information
ostcar committed Apr 20, 2022
1 parent f2ac228 commit b81eb37
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 124 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ docker run --network host --env OPENSLIDES_DEVELOPMENT=true openslides-vote-dev

### Start a Poll

To start a poll a POST request has to be send to the create-url.
To start a poll a POST request has to be send to the start-url.

To send the same request twice is ok.

```
curl -X POST localhost:9013/internal/vote/create?id=1
curl -X POST localhost:9013/internal/vote/start?id=1
```


Expand Down
12 changes: 6 additions & 6 deletions internal/vote/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const (
httpPathExternal = "/system/vote"
)

type creater interface {
Create(ctx context.Context, pollID int) error
type starter interface {
Start(ctx context.Context, pollID int) error
}

func handleCreate(mux *http.ServeMux, create creater) {
func handleStart(mux *http.ServeMux, start starter) {
mux.HandleFunc(
httpPathInternal+"/create",
httpPathInternal+"/start",
func(w http.ResponseWriter, r *http.Request) {
log.Info("Receiving create request")
log.Info("Receiving start request")
w.Header().Set("Content-Type", "application/json")

if r.Method != "POST" {
Expand All @@ -39,7 +39,7 @@ func handleCreate(mux *http.ServeMux, create creater) {
return
}

if err := create.Create(r.Context(), id); err != nil {
if err := start.Start(r.Context(), id); err != nil {
handleError(w, err, true)
return
}
Expand Down
20 changes: 10 additions & 10 deletions internal/vote/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ import (
"testing"
)

type createrStub struct {
type starterStub struct {
id int
expectErr error
}

func (c *createrStub) Create(ctx context.Context, pollID int) error {
func (c *starterStub) Start(ctx context.Context, pollID int) error {
c.id = pollID
return c.expectErr
}

func TestHandleCreate(t *testing.T) {
creater := &createrStub{}
func TestHandleStart(t *testing.T) {
starter := &starterStub{}

url := "/internal/vote/create"
url := "/internal/vote/start"
mux := http.NewServeMux()
handleCreate(mux, creater)
handleStart(mux, starter)

t.Run("Get request", func(t *testing.T) {
resp := httptest.NewRecorder()
Expand Down Expand Up @@ -63,13 +63,13 @@ func TestHandleCreate(t *testing.T) {
t.Errorf("Got status %s, expected 200 - OK", resp.Result().Status)
}

if creater.id != 1 {
t.Errorf("Creater was called with id %d, expected 1", creater.id)
if starter.id != 1 {
t.Errorf("Start was called with id %d, expected 1", starter.id)
}
})

t.Run("Exist error", func(t *testing.T) {
creater.expectErr = ErrExists
starter.expectErr = ErrExists

resp := httptest.NewRecorder()
mux.ServeHTTP(resp, httptest.NewRequest("POST", url+"?id=1", strings.NewReader("request body")))
Expand All @@ -92,7 +92,7 @@ func TestHandleCreate(t *testing.T) {
})

t.Run("Internal error", func(t *testing.T) {
creater.expectErr = errors.New("TEST_Error")
starter.expectErr = errors.New("TEST_Error")

resp := httptest.NewRecorder()
mux.ServeHTTP(resp, httptest.NewRequest("POST", url+"?id=1", strings.NewReader("request body")))
Expand Down
2 changes: 1 addition & 1 deletion internal/vote/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Run(ctx context.Context, environment []string, getSecret func(name string)
service := New(fastBackend, longBackend, ds, counter)

mux := http.NewServeMux()
handleCreate(mux, service)
handleStart(mux, service)
handleStop(mux, service)
handleClear(mux, service)
handleClearAll(mux, service)
Expand Down
16 changes: 6 additions & 10 deletions internal/vote/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func (v *Vote) backend(p pollConfig) Backend {
return backend
}

// Create an electronic vote.
// Start an electronic vote.
//
// This function is idempotence. If you call it with the same input, you will
// get the same output. This means, that when a poll is stopped, Create() will
// get the same output. This means, that when a poll is stopped, Start() will
// not throw an error.
func (v *Vote) Create(ctx context.Context, pollID int) (err error) {
log.Debug("Receive create event for poll %d", pollID)
func (v *Vote) Start(ctx context.Context, pollID int) (err error) {
log.Debug("Receive start event for poll %d", pollID)
defer func() {
log.Debug("End create event with error: %v", err)
log.Debug("End start event with error: %v", err)
}()

recorder := datastore.NewRecorder(v.ds)
Expand All @@ -60,11 +60,7 @@ func (v *Vote) Create(ctx context.Context, pollID int) (err error) {
}

if poll.pollType == "analog" {
return MessageError{ErrInvalid, "Analog poll can not be created"}
}

if poll.state != "started" {
return MessageError{ErrInternal, fmt.Sprintf("Poll state is %s, only started polls can be created", poll.state)}
return MessageError{ErrInvalid, "Analog poll can not be started"}
}

if err := poll.preload(ctx, ds); err != nil {
Expand Down

0 comments on commit b81eb37

Please sign in to comment.