-
-
Notifications
You must be signed in to change notification settings - Fork 182
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
Get records API route #178
Open
qdm12
wants to merge
9
commits into
master
Choose a base branch
from
get-records
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9a69b5a
Marshal JSON method for all record settings
qdm12 9167522
GET /api/v1/records route
qdm12 21acfac
Update new providers added
qdm12 d2f113b
Return an empty slice if no data is set
qdm12 725b677
Database mocks
qdm12 18e4581
SPDyn marshal json method
qdm12 532a096
Settings mock
qdm12 ade2de9
Get records unit tests
qdm12 9da4365
Check accept header
qdm12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package server | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
func (h *handlers) getRecords(w http.ResponseWriter, r *http.Request) { | ||
accept := r.Header.Get("Accept") | ||
var contentType string | ||
switch accept { | ||
case "", "application/json": | ||
contentType = "application/json" | ||
default: | ||
httpError(w, http.StatusBadRequest, `content type "`+accept+`" is not supported`) | ||
} | ||
w.Header().Set("Content-Type", contentType) | ||
|
||
records := h.db.SelectAll() | ||
encoder := json.NewEncoder(w) | ||
// TODO check Accept header and return Content-Type header | ||
if err := encoder.Encode(records); err != nil { | ||
h.logger.Error(err) | ||
httpError(w, http.StatusInternalServerError, "") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package server | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/qdm12/ddns-updater/internal/constants" | ||
"github.com/qdm12/ddns-updater/internal/data/mock_data" | ||
"github.com/qdm12/ddns-updater/internal/models" | ||
"github.com/qdm12/ddns-updater/internal/records" | ||
"github.com/qdm12/ddns-updater/internal/settings/mock_settings" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_handlers_getRecords(t *testing.T) { | ||
t.Parallel() | ||
|
||
exampleTime := time.Unix(1000, 0) | ||
|
||
exampleRecord := records.Record{ | ||
History: models.History{ | ||
models.HistoryEvent{ | ||
IP: net.IP{127, 0, 0, 1}, | ||
Time: exampleTime, | ||
}, | ||
}, | ||
Status: constants.SUCCESS, | ||
Message: "message", | ||
Time: exampleTime, | ||
LastBan: &exampleTime, | ||
} | ||
|
||
testCases := map[string]struct { | ||
acceptHeader string | ||
records []records.Record | ||
responseBody string | ||
}{ | ||
"empty records": { | ||
records: []records.Record{}, | ||
responseBody: "[]\n", | ||
}, | ||
"single record": { | ||
records: []records.Record{ | ||
exampleRecord, | ||
}, | ||
responseBody: `[{"Settings":{"a":{}},"History":[{"ip":"127.0.0.1","time":"1970-01-01T00:16:40Z"}],"Status":"success","Message":"message","Time":"1970-01-01T00:16:40Z","LastBan":"1970-01-01T00:16:40Z"}]` + "\n", // nolint:lll | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
ctrl := gomock.NewController(t) | ||
|
||
// Set the settings interface on each record | ||
for i := range testCase.records { | ||
settings := mock_settings.NewMockSettings(ctrl) | ||
const json = `{"a":{}}` | ||
settings.EXPECT().MarshalJSON().Return([]byte(json), nil) | ||
testCase.records[i].Settings = settings | ||
} | ||
|
||
db := mock_data.NewMockDatabase(ctrl) | ||
db.EXPECT().SelectAll().Return(testCase.records) | ||
|
||
handlers := &handlers{ | ||
db: db, | ||
} | ||
|
||
w := httptest.NewRecorder() | ||
r := &http.Request{ | ||
Header: http.Header{ | ||
"Accept": []string{testCase.acceptHeader}, | ||
}, | ||
} | ||
|
||
handlers.getRecords(w, r) | ||
|
||
response := w.Result() | ||
assert.Equal(t, http.StatusOK, response.StatusCode) | ||
assert.Equal(t, testCase.responseBody, w.Body.String()) | ||
_ = response.Body.Close() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should you not return here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is just sending an http response as json
{"error": "message"}
with the status code given 😉I'll get to authentication soon to merge that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair point 🤣. I reviewed the rest, all seem alright to me. I learnt about Read Locks, it seemed weird to me to have to lock in order to read. Regarding authentication, a cool thing could be to make ddns-updater a microservice that implements oauth2. We could interface it with the user management interface we want, we wouldn't have to maintain user management. I'm personally using Authelia which has oauth2 in beta. There is also https://github.com/netlify/gotrue