Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
describe('message archive plugin', () => {
let chatConnectionService: XmppChatConnectionService;
let chatAdapter: XmppChatAdapter;
let contactFactory: ContactFactoryService;
let xmppClientMock: SpyObj;
let contact1: Contact;
const userJid = parseJid('me@example.com/myresource');
const validArchiveStanza =
xml('message', {},
xml('result', {xmlns: 'urn:xmpp:mam:2'},
xml('forwarded', {},
xml('delay', {stamp: '2018-07-18T08:47:44.233057Z'}),
xml('message', {to: userJid.toString(), from: 'someone@else.com/resource'},
xml('origin-id', {id: 'id'}),
xml('body', {}, 'message text')))));
beforeEach(() => {
const mockClientFactory = new MockClientFactory();
xmppClientMock = mockClientFactory.clientInstance;
TestBed.configureTestingModule({
providers: [
XmppChatConnectionService,
{provide: XmppClientFactoryService, useValue: mockClientFactory},
XmppChatAdapter,
{provide: LogService, useValue: testLogService()},
ContactFactoryService
]
});
private sendAddToRoster(jid: string) {
return this.chatService.chatConnectionService.sendIq(
xml('iq', {type: 'set'},
xml('query', {xmlns: 'jabber:iq:roster'},
xml('item', {jid}))));
}
private convertSavedConferenceToElement(savedConference: SavedConference) {
const {name, autojoin, jid} = savedConference;
return xml('conference', {name, jid, autojoin: autojoin.toString()});
}
toStanza() {
return xml('iq', {type: 'get'},
xml('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'},
xml('items', {node: this.node})
)
);
}
domain = domain || getDomain(service);
this.logService.debug('registration plugin', 'connecting...');
await this.connect(username, password, service, domain);
this.logService.debug('registration plugin', 'connection established, starting registration');
await this.client.iqCaller.request(
xml('iq', {type: 'get', to: domain},
xml('query', {xmlns: 'jabber:iq:register'})
)
);
this.logService.debug('registration plugin', 'server acknowledged registration request, sending credentials');
await this.client.iqCaller.request(
xml('iq', {type: 'set'},
xml('query', {xmlns: 'jabber:iq:register'},
xml('username', {}, username),
xml('password', {}, password)
)
)
);
this.registered$.next();
await this.loggedIn$.pipe(takeUntil(this.cleanUp), first()).toPromise();
this.logService.debug('registration plugin', 'registration successful');
})(), this.registrationTimeout);
} catch (e) {
if (content.attrs.to === 'jabber.example.com'
&& content.getChild('query').attrs.xmlns === 'http://jabber.org/protocol/disco#items') {
chatConnectionService.onStanzaReceived(
xml('iq', {type: 'result', id: content.attrs.id},
xml('query', {xmlns: 'http://jabber.org/protocol/disco#items'},
xml('item', {jid: 'conference.jabber.example.com'})
)
) as Stanza
);
} else if (content.getChild('query') && content.getChild('query').attrs.xmlns === 'http://jabber.org/protocol/disco#info') {
if (content.attrs.to === 'conference.jabber.example.com') {
chatConnectionService.onStanzaReceived(
xml('iq', {type: 'result', id: content.attrs.id, from: content.attrs.to},
xml('query', {xmlns: 'http://jabber.org/protocol/disco#info'},
xml('identity', {type: 'text', category: 'conference'})
)
) as Stanza
);
} else {
chatConnectionService.onStanzaReceived(
xml('iq', {type: 'result', id: content.attrs.id, from: content.attrs.to},
xml('query', {xmlns: 'http://jabber.org/protocol/disco#info'},
xml('identity', {type: 'type', category: 'category'})
)
) as Stanza
);
}
} else {
fail('unexpected stanza: ' + content.toString());
}
return Promise.resolve();
private convertConfiguration(configurationKeyValuePair: { [key: string]: string[] }) {
const configurationFields = [];
for (const configurationKey in configurationKeyValuePair) {
if (configurationKeyValuePair.hasOwnProperty(configurationKey)) {
const configurationValues = configurationKeyValuePair[configurationKey].map(value => xml('value', {}, value));
configurationFields.push(
xml('field', {var: configurationKey}, ...configurationValues)
);
}
}
return configurationFields;
}
private sendMessageStateNotification(recipient: JID, messageId: string, state: MessageState) {
const messageStateResponse = xml('message', {
to: recipient.bare().toString(),
from: this.xmppChatAdapter.chatConnectionService.userJid.toString(),
type: 'chat'
},
xml('message-state', {
xmlns: STORAGE_NGX_CHAT_CONTACT_MESSAGE_STATES,
messageId,
date: new Date().toISOString(),
state
})
);
this.xmppChatAdapter.chatConnectionService.send(messageStateResponse);
}
private sendMessageStateNotification(recipient: JID, messageId: string, state: MessageState) {
const messageStateResponse = xml('message', {
to: recipient.bare().toString(),
from: this.xmppChatAdapter.chatConnectionService.userJid.toString(),
type: 'chat'
},
xml('message-state', {
xmlns: STORAGE_NGX_CHAT_CONTACT_MESSAGE_STATES,
messageId,
date: new Date().toISOString(),
state
})
);
this.xmppChatAdapter.chatConnectionService.send(messageStateResponse);
}
const configurationListElement = configurationForm.getChild('query').getChild('x');
if (!configurationListElement) {
throw new Error('room not configurable');
}
const configurationKeyValuePair = {
...this.extractDefaultConfiguration(configurationListElement.getChildren('field')),
...this.extractRoomCreationRequestConfiguration(request)
};
try {
await this.xmppChatAdapter.chatConnectionService.sendIq(
xml('iq', {type: 'set', to: room.roomJid.toString()},
xml('query', {xmlns: 'http://jabber.org/protocol/muc#owner'},
xml('x', {xmlns: 'jabber:x:data', type: 'submit'},
xml('field', {var: 'FORM_TYPE'},
xml('value', {}, 'http://jabber.org/protocol/muc#roomconfig')
),
...this.convertConfiguration(configurationKeyValuePair)
)
)
)
);
return room;
} catch (e) {
throw new Error('room configuration rejected: ' + e.toString());
}
}