feat: access ActivityPub client through interfaces to facilitate mocking in unit tests (#4853)
Was facing issues while writing unit tests for federation code. Mocks weren't catching all network calls, because was being out of scope of the mocking infra. Plus, I think we can have more granular tests. This PR puts the client behind an interface, that can be retrieved from `ctx`. Context doesn't require initialization, as it defaults to the implementation available in-tree. It may be overridden when required (like testing). ## Mechanism 1. Get client factory from `ctx` (factory contains network and crypto parameters that are needed) 2. Initialize client with sender's keys and the receiver's public key 3. Use client as before. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4853 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Aravinth Manivannan <realaravinth@batsense.net> Co-committed-by: Aravinth Manivannan <realaravinth@batsense.net>
This commit is contained in:
		
							parent
							
								
									1ddf44edd6
								
							
						
					
					
						commit
						f9cbea3d6b
					
				
					 6 changed files with 140 additions and 25 deletions
				
			
		| 
						 | 
				
			
			@ -64,14 +64,19 @@ Set up a user called "me" for all tests
 | 
			
		|||
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
func TestNewClientReturnsClient(t *testing.T) {
 | 
			
		||||
func TestClientCtx(t *testing.T) {
 | 
			
		||||
	require.NoError(t, unittest.PrepareTestDatabase())
 | 
			
		||||
	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
 | 
			
		||||
	pubID := "myGpgId"
 | 
			
		||||
	c, err := NewClient(db.DefaultContext, user, pubID)
 | 
			
		||||
	cf, err := NewClientFactory()
 | 
			
		||||
	log.Debug("ClientFactory: %v\nError: %v", cf, err)
 | 
			
		||||
	require.NoError(t, err)
 | 
			
		||||
 | 
			
		||||
	c, err := cf.WithKeys(db.DefaultContext, user, pubID)
 | 
			
		||||
 | 
			
		||||
	log.Debug("Client: %v\nError: %v", c, err)
 | 
			
		||||
	require.NoError(t, err)
 | 
			
		||||
	_ = NewContext(db.DefaultContext, cf)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* TODO: bring this test to work or delete
 | 
			
		||||
| 
						 | 
				
			
			@ -109,7 +114,9 @@ func TestActivityPubSignedPost(t *testing.T) {
 | 
			
		|||
	require.NoError(t, unittest.PrepareTestDatabase())
 | 
			
		||||
	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
 | 
			
		||||
	pubID := "https://example.com/pubID"
 | 
			
		||||
	c, err := NewClient(db.DefaultContext, user, pubID)
 | 
			
		||||
	cf, err := NewClientFactory()
 | 
			
		||||
	require.NoError(t, err)
 | 
			
		||||
	c, err := cf.WithKeys(db.DefaultContext, user, pubID)
 | 
			
		||||
	require.NoError(t, err)
 | 
			
		||||
 | 
			
		||||
	expected := "BODY"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue