generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mocks and unit tests to http and msgraph packages (#2)
* command + http framework * Got a 404 on the hardcoded Mattermost Org - no Outlook * `/msoffice viewcal` works on a personal account - added OAuth2 credentials a config values * PR feedback * Add mocks and unit tests to http and msgraph packages Added a mocks package that mocks out the functionality of some the interfaces needed by the oauth handlers. This allows testing of each error check within the oauth handler functions. I also added github.com/jarcoal/httpmock as a dependecy to test both the oauth code to token exchange and each of the calls that will be made out to the microsoft graph API. The tests cover how all errors are encountered for the oauth connection. They also ensure that access token refreshing happens when the token is expired. This tests to make sure the golang oauth client works out of the box for this plugin. * Update http.go and plugin.go with upstream/dev branch * Address PR review comments This includes moving all of the tests to their own subpackages to allow for a cleaner workspace. Move all the mock interfaces to the test_http package as they are only needed there currently. Update mock interfaces to use github.com/stretchr/testify/mock to conform to Mattermost mock standards. Also addresses any naming convention comments in this commit as well. * Migrate mocks to use gomock Per @cpoile we are migrating the mocked interfaces from stretchr/testify/mock to instead be generated and used via gomock. I have added a new makefile target to generate the mock interfaces using mockgen. In addition I have generated those mocks to be used in the unit tests in the test_http package. All of those unit tests have been updated to use the generated mocks as well. * Use getUserRequest for oauth connect tests * Refactor how mocks are built for tests * Refactor test case mocks, fix minor nitpicks Refactored the mocks to be a part of the testcase struct and then pass those to a `setupMocks` function which allow for custom mock behavior. This sigificantly reduces the code for setup and allows for more customizable mocks. Also addressing some of @levb comments on package names for the test folders and naming convention. * Create mocks in test run function and pass to tc struct after setup * Move redundant test case fields to testing block
- Loading branch information
Showing
16 changed files
with
927 additions
and
9 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package testhttp | ||
|
||
import "net/http" | ||
|
||
var ( | ||
_ http.ResponseWriter = &mockResponseWriter{} | ||
) | ||
|
||
func defaultMockResponseWriter() *mockResponseWriter { | ||
return &mockResponseWriter{ | ||
HeaderMap: make(http.Header), | ||
Bytes: make([]byte, 0), | ||
Err: nil, | ||
StatusCode: http.StatusOK, | ||
} | ||
} | ||
|
||
type mockResponseWriter struct { | ||
HeaderMap http.Header | ||
Bytes []byte | ||
Err error | ||
StatusCode int | ||
} | ||
|
||
func (rw *mockResponseWriter) Header() http.Header { | ||
return rw.HeaderMap | ||
} | ||
|
||
func (rw *mockResponseWriter) Write(bytes []byte) (int, error) { | ||
if rw.Err != nil { | ||
return 0, rw.Err | ||
} | ||
|
||
rw.Bytes = append(rw.Bytes, bytes...) | ||
|
||
return len(rw.Bytes), nil | ||
} | ||
|
||
func (rw *mockResponseWriter) WriteHeader(statusCode int) { | ||
rw.StatusCode = statusCode | ||
} |
Oops, something went wrong.