()
| 781 | } |
| 782 | |
| 783 | func (ts *MiddlewareTestSuite) TestDatabaseCleanup() { |
| 784 | testHandler := func(statusCode int) http.HandlerFunc { |
| 785 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 786 | w.WriteHeader(statusCode) |
| 787 | b, _ := json.Marshal(map[string]interface{}{"message": "ok"}) |
| 788 | w.Write([]byte(b)) |
| 789 | }) |
| 790 | } |
| 791 | |
| 792 | cases := []struct { |
| 793 | desc string |
| 794 | statusCode int |
| 795 | method string |
| 796 | }{ |
| 797 | { |
| 798 | desc: "Run cleanup successfully", |
| 799 | statusCode: http.StatusOK, |
| 800 | method: http.MethodPost, |
| 801 | }, |
| 802 | { |
| 803 | desc: "Skip cleanup if GET", |
| 804 | statusCode: http.StatusOK, |
| 805 | method: http.MethodGet, |
| 806 | }, |
| 807 | { |
| 808 | desc: "Skip cleanup if 3xx", |
| 809 | statusCode: http.StatusSeeOther, |
| 810 | method: http.MethodPost, |
| 811 | }, |
| 812 | { |
| 813 | desc: "Skip cleanup if 4xx", |
| 814 | statusCode: http.StatusBadRequest, |
| 815 | method: http.MethodPost, |
| 816 | }, |
| 817 | { |
| 818 | desc: "Skip cleanup if 5xx", |
| 819 | statusCode: http.StatusInternalServerError, |
| 820 | method: http.MethodPost, |
| 821 | }, |
| 822 | } |
| 823 | |
| 824 | mockCleanup := new(MockCleanup) |
| 825 | mockCleanup.On("Clean", mock.Anything).Return(0, nil) |
| 826 | for _, c := range cases { |
| 827 | ts.Run("DatabaseCleanup", func() { |
| 828 | req := httptest.NewRequest(c.method, "http://localhost", nil) |
| 829 | w := httptest.NewRecorder() |
| 830 | ts.API.databaseCleanup(mockCleanup)(testHandler(c.statusCode)).ServeHTTP(w, req) |
| 831 | require.Equal(ts.T(), c.statusCode, w.Code) |
| 832 | }) |
| 833 | } |
| 834 | mockCleanup.AssertNumberOfCalls(ts.T(), "Clean", 1) |
| 835 | } |
| 836 | |
| 837 | func (ts *MiddlewareTestSuite) TestRequireAdminCredentialsSessionCheck() { |
| 838 | models.TruncateAll(ts.API.db) |
nothing calls this directly
no test coverage detected