• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vocdoni / saas-backend / 17557469823

08 Sep 2025 04:25PM UTC coverage: 58.777% (-0.06%) from 58.841%
17557469823

Pull #213

github

altergui
fix
Pull Request #213: api: standardize parameters ProcessID, CensusID, GroupID, JobID, UserID, BundleID

254 of 345 new or added lines in 22 files covered. (73.62%)

19 existing lines in 7 files now uncovered.

5652 of 9616 relevant lines covered (58.78%)

32.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

51.47
/api/process.go
1
package api
2

3
import (
4
        "encoding/json"
5
        "net/http"
6

7
        "github.com/vocdoni/saas-backend/api/apicommon"
8
        "github.com/vocdoni/saas-backend/db"
9
        "github.com/vocdoni/saas-backend/errors"
10
)
11

12
// createProcessHandler godoc
13
//
14
//        @Summary                Create a new voting process
15
//        @Description        Create a new voting process. Requires Manager/Admin role.
16
//        @Tags                        process
17
//        @Accept                        json
18
//        @Produce                json
19
//        @Security                BearerAuth
20
//        @Param                        processId        path                string                                                        true        "Process ID"
21
//        @Param                        request                body                apicommon.CreateProcessRequest        true        "Process creation information"
22
//        @Success                200                        {string}        string                                                        "OK"
23
//        @Failure                400                        {object}        errors.Error                                        "Invalid input data"
24
//        @Failure                401                        {object}        errors.Error                                        "Unauthorized"
25
//        @Failure                404                        {object}        errors.Error                                        "Published census not found"
26
//        @Failure                409                        {object}        errors.Error                                        "Process already exists"
27
//        @Failure                500                        {object}        errors.Error                                        "Internal server error"
28
//        @Router                        /process/{processId} [post]
29
func (a *API) createProcessHandler(w http.ResponseWriter, r *http.Request) {
3✔
30
        processID, err := apicommon.ProcessIDFromRequest(r)
3✔
31
        if err != nil {
4✔
32
                errors.ErrMalformedURLParam.WithErr(err).Write(w)
1✔
33
                return
1✔
34
        }
1✔
35

36
        processInfo := &apicommon.CreateProcessRequest{}
2✔
37
        if err := json.NewDecoder(r.Body).Decode(&processInfo); err != nil {
2✔
38
                errors.ErrMalformedBody.Write(w)
×
39
                return
×
40
        }
×
41

42
        if processInfo.PublishedCensusRoot == nil || processInfo.CensusID.IsZero() {
3✔
43
                errors.ErrMalformedBody.Withf("missing published census root or ID").Write(w)
1✔
44
                return
1✔
45
        }
1✔
46

47
        // get the user from the request context
48
        user, ok := apicommon.UserFromContext(r.Context())
1✔
49
        if !ok {
1✔
50
                errors.ErrUnauthorized.Write(w)
×
51
                return
×
52
        }
×
53

54
        census, err := a.db.Census(processInfo.CensusID)
1✔
55
        if err != nil {
1✔
56
                if err == db.ErrNotFound {
×
57
                        errors.ErrMalformedURLParam.Withf("census not found").Write(w)
×
58
                        return
×
59
                }
×
60
                errors.ErrGenericInternalServerError.WithErr(err).Write(w)
×
61
                return
×
62
        }
63

64
        if processInfo.PublishedCensusRoot.String() != census.Published.Root.String() ||
1✔
65
                processInfo.PublishedCensusURI != census.Published.URI {
1✔
66
                errors.ErrMalformedBody.Withf("published census root or URI does not match census").Write(w)
×
67
                return
×
68
        }
×
69

70
        // check the user has the necessary permissions
71
        if !user.HasRoleFor(census.OrgAddress, db.ManagerRole) && !user.HasRoleFor(census.OrgAddress, db.AdminRole) {
1✔
72
                errors.ErrUnauthorized.Withf("user is not admin of organization").Write(w)
×
73
                return
×
74
        }
×
75

76
        // check that the process does not exist
77
        if _, err := a.db.Process(processID); err == nil {
1✔
78
                errors.ErrDuplicateConflict.Withf("process already exists").Write(w)
×
79
                return
×
80
        }
×
81

82
        // finally create the process
83
        process := &db.Process{
1✔
84
                ID:         processID,
1✔
85
                Census:     *census,
1✔
86
                Metadata:   processInfo.Metadata,
1✔
87
                OrgAddress: census.OrgAddress,
1✔
88
        }
1✔
89

1✔
90
        if err := a.db.SetProcess(process); err != nil {
1✔
91
                errors.ErrGenericInternalServerError.WithErr(err).Write(w)
×
92
                return
×
93
        }
×
94

95
        apicommon.HTTPWriteOK(w)
1✔
96
}
97

98
// processInfoHandler godoc
99
//
100
//        @Summary                Get process information
101
//        @Description        Retrieve voting process information by ID. Returns process details including census and metadata.
102
//        @Tags                        process
103
//        @Accept                        json
104
//        @Produce                json
105
//        @Param                        processId        path                string        true        "Process ID"
106
//        @Success                200                        {object}        db.Process
107
//        @Failure                400                        {object}        errors.Error        "Invalid process ID"
108
//        @Failure                404                        {object}        errors.Error        "Process not found"
109
//        @Failure                500                        {object}        errors.Error        "Internal server error"
110
//        @Router                        /process/{processId} [get]
111
func (a *API) processInfoHandler(w http.ResponseWriter, r *http.Request) {
1✔
112
        processID, err := apicommon.ProcessIDFromRequest(r)
1✔
113
        if err != nil {
2✔
114
                errors.ErrMalformedURLParam.WithErr(err).Write(w)
1✔
115
                return
1✔
116
        }
1✔
117

NEW
118
        process, err := a.db.Process(processID)
×
UNCOV
119
        if err != nil {
×
UNCOV
120
                if err == db.ErrNotFound {
×
121
                        errors.ErrMalformedURLParam.Withf("process not found").Write(w)
×
122
                        return
×
123
                }
×
UNCOV
124
                errors.ErrGenericInternalServerError.WithErr(err).Write(w)
×
UNCOV
125
                return
×
126
        }
127

128
        apicommon.HTTPWriteJSON(w, process)
×
129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc